POJ1797(dijkstra求最短最长边)

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 26442   Accepted: 7044

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4
题意:求结点1到结点n的所有每个路径中的最小承载量中的最大值(与:所有路径中最长边的最短值相区别)。
/*    
    dijkstra Accepted    4316 KB    391 ms
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=1002;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;

int dijkstra(int s)
{
    int vis[MAXN];
    memset(vis,0,sizeof(vis));
    int d[MAXN];//代表最大承载量 
    for(int i=1;i<=V;i++)    d[i]=mp[s][i];
    
    int n=V;
    while(n--)
    {
        int maxcost,k;
        maxcost=0;
        for(int i=1;i<=V;i++)
        {
            if(!vis[i]&&d[i]>maxcost)
            {
                k=i;
                maxcost=d[i];
            }
        }
        
        vis[k]=1;
        for(int i=1;i<=V;i++)
        {
            if(!vis[i]&&d[i]<min(d[k],mp[k][i]))
            {
                d[i]=min(d[k],mp[k][i]);
            }
        }
    }
    return d[V];
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        memset(mp,0,sizeof(mp));//承载量初始化为0
        scanf("%d%d",&V,&E);
        for(int i=0;i<E;i++)
        {
            int u,v,cost;
            scanf("%d%d%d",&u,&v,&cost);
            mp[u][v]=mp[v][u]=cost;
        }
        
                
        printf("Scenario #%d:
",cas);
        printf("%d

",dijkstra(1));
    }
    
    return 0;
}
/*
    floyd Time Limit Exceeded
*/
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=1002;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int V,E;
        scanf("%d%d",&V,&E);
        for(int i=1;i<=V;i++)
            for(int j=1;j<=V;j++)
                if(i==j)    mp[i][j]=0;
                else    mp[i][j]=INF;    
        for(int i=0;i<E;i++)
        {
            int u,v,cost;
            scanf("%d%d%d",&u,&v,&cost);
            mp[u][v]=mp[v][u]=cost;
        }
        
        for(int k=1;k<=V;k++)
            for(int i=1;i<=V;i++)
                for(int j=1;j<=V;j++)
                    if(mp[i][k]<mp[i][j]&&mp[k][j]<mp[i][j])
                        mp[i][j]=max(mp[i][k],mp[k][j]);
        
        printf("Scenario #%d:
",cas);
        printf("%d

",mp[1][V]);
    }
    
    return 0;
}
/*
    堆优化dijkstra 1797    Accepted    5224K    329MS     
*/
#include"cstdio"
#include"queue"
#include"vector"
#include"algorithm"
#include"cstring"
using namespace std;
const int MAXN=1002;
const int INF=0x3fffffff;
struct Edge{
    int to,cost;
    Edge(int to,int cost)
    {
        this->to=to;
        this->cost=cost;
    }
    friend bool operator<(const Edge &a,const Edge &b)
    {
        return a.cost < b.cost;
    }
};
int V,E;
vector<int> G[MAXN];
int mp[MAXN][MAXN];
int dijkstra(int s)
{
    int d[MAXN];
    priority_queue<Edge> que;

    for(int i=1;i<=V;i++)    
    {
        que.push(Edge(i,mp[s][i]));
        d[i]=mp[s][i];
    }    
    while(!que.empty())
    {
        Edge e=que.top();que.pop();
        if(e.to==V)    return e.cost;
        
        int v=e.to;
        if(d[v]>e.cost)    continue;
        for(int i=0;i<G[v].size();i++)
        {
            int u=G[v][i];
            if(d[u]<min(d[v],mp[v][u]))
            {
                d[u]=min(d[v],mp[v][u]);
                que.push(Edge(u,d[u]));
            }            
        }
    }
    return -1;
}
int main()
{
    
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        memset(mp,0,sizeof(mp));
        scanf("%d%d",&V,&E);
        for(int i=1;i<=V;i++)
        {
            G[i].clear();
        }
        for(int i=0;i<E;i++)
        {
            int u,v,cost;
            scanf("%d%d%d",&u,&v,&cost);
            G[u].push_back(v);
            G[v].push_back(u);
            mp[u][v]=mp[v][u]=cost;
        }    
        printf("Scenario #%d:
",cas);
        printf("%d

",dijkstra(1));
    }
    
    return 0;
}

 二分+bfs

/*
    1797    Accepted    1420K    329MS    C++
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAXN=1005;
const int INF=0x3f3f3f3f;
struct Edge{
    int to,w;
    Edge(){}
    Edge(int to,int w)
    {
        this->to=to;
        this->w=w;
    }
};
int n,m;
vector<Edge> arc[MAXN];
int src,ter,vis[MAXN];
bool bfs(int limit)
{
    memset(vis,0,sizeof(vis));
    queue<int> que;
    que.push(src);
    vis[src]=1;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        if(u==ter)    return true;
        for(int i=0,size=arc[u].size();i<size;i++)
        {
            Edge e=arc[u][i];
            if(!vis[e.to]&&limit<=e.w)
            {
                vis[e.to]=1;
                que.push(e.to);
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);    
    for(int cas=1;cas<=T;cas++)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)    arc[i].clear();
        src=1;
        ter=n;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            arc[u].push_back(Edge(v,w));
            arc[v].push_back(Edge(u,w));
        }
        int l=0,r=INF;
        while(r-l>1)
        {
            int mid=(l+r)/2;
            if(bfs(mid))
            {
                l=mid;
            }
            else
            {
                r=mid;
            }
        }
        printf("Scenario #%d:
",cas);
        printf("%d

",l);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5146914.html