POJ

来源:互联网 发布:智能电视网络dns设置 编辑:程序博客网 时间:2024/05/19 18:10

Heavy Transportation

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

题意:有n个城市分别以1~n命名,这些城市之间有m条道路,每条道路都有一个承载量。让你找一条路径,使城市1到城市n路径中承载量最小的道路尽可能的大。

思路:各种最短路模板该条件或者求最大路。下面有dijkstra/dijkstra-stl优化/SPFA/prime/kruskal版本

最短路

//dijkstra------407ms#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<stack>#include<algorithm>#define inf 99999999using namespace std;const int MAX=1000+10;int map[MAX][MAX],dis[MAX],book[MAX];void dijkstra(int n){    int i,j,max,t;    for(i=1;i<=n;i++){        dis[i]=map[1][i];        book[i]=0;    }    dis[1]=0;    for(i=1;i<=n;i++){        max=-1;        for(j=1;j<=n;j++)            if(book[j]!=1&&dis[j]>max){                max=dis[j];                t=j;            }        book[t]=1;        for(j=1;j<=n;j++)            if(book[j]!=1&&dis[j]<min(dis[t],map[t][j]))                dis[j]=min(dis[t],map[t][j]);    }}int main(){    //freopen("in.in","r",stdin);    int t,i,k,n,m,u,v,w;    scanf("%d",&t);    for(k=1;k<=t;k++){        memset(map,0,sizeof(map));        scanf("%d%d",&n,&m);        for(i=1;i<=m;i++){            scanf("%d%d%d",&u,&v,&w);            map[u][v]=map[v][u]=w;        }        dijkstra(n);        printf("Scenario #%d:\n",k);        printf("%d\n\n",dis[n]);    }    return 0;}
//dijkstra优先队列优化-----313ms#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0X3f3f3f3fusing namespace std;typedef long long ll;const int MAXN=1e6+10;const int MAX=2000+10;//最短路径-----dijkstra的stl优化 时间复杂度O(elog(v))int n,m;int dis[MAX];int first[MAX],num;struct EDGE{    int v,w,next;}edge[MAXN*2];          //链式向前星typedef pair<int,int>P;             //stl储存void init(){    fill(dis,dis+n+1,0);            //memset只能初始化-1,0,1    memset(first,-1,sizeof(first));    num=0;}void addedge(int u,int v,int w){    edge[num].v=v;    edge[num].w=w;    edge[num].next=first[u];    first[u]=num++;}int dijkstra(int s,int g){    priority_queue<P,vector<P>,less<P> >q;      dis[s]=inf;    q.push(P(0,s));    while(!q.empty()){        P t=q.top();        q.pop();        int u=t.second;        for(int i=first[u];i!=-1;i=edge[i].next){            int v=edge[i].v;            int w=edge[i].w;            if(dis[v]<min(dis[u],w)){                dis[v]=min(dis[u],w);                q.push(P(dis[v],v));            }        }    }    return dis[g];}int main(){    #ifdef ONLINE_JUDGE    #else    freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    #endif    int T;    int flag=1;    cin>>T;    while(T--){        init();        cin>>n>>m;        int u,v,w;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w);            addedge(v,u,w);        }        printf("Scenario #%d:\n%d\n\n",flag++,dijkstra(1,n));    }    return 0;}
//SPFA-------313ms#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0X3f3f3f3fusing namespace std;typedef long long ll;const int MAXN=1e6+10;const int MAX=2000+10;int n,m;int first[MAX],num;int dis[MAX],vis[MAX],cnt[MAX];struct EDGE{    int v,w,next;}edge[MAXN*2];void init(){    num=0;    memset(first,-1,sizeof(first));    memset(vis,0,sizeof(vis));    memset(cnt,0,sizeof(cnt));}void addedge(int u,int v,int w){    edge[num].v=v;    edge[num].w=w;    edge[num].next=first[u];    first[u]=num++; }int spfa(int s,int g){    queue<int>q;    fill(dis,dis+n+1,0);    dis[s]=inf;    vis[s]=1;    cnt[s]++;    q.push(s);    while(!q.empty()){        int u=q.front();        q.pop();        vis[u]=0;        for(int i=first[u];i!=-1;i=edge[i].next){            int v=edge[i].v;            int w=edge[i].w;            if(u==1){            }            if(dis[v]<min(dis[u],w)){                dis[v]=min(dis[u],w);                if(!vis[v]){                    vis[v]=1;                    q.push(v);                    cnt[v]++;                    //if(cnt[v]>n)                    //  return -1;                }            }        }    }    return dis[g];}int main(){    #ifdef ONLINE_JUDGE    #else    freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    #endif    int T;    int flag=1;    cin>>T;    while(T--){        init();        cin>>n>>m;        int u,v,w;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w);            addedge(v,u,w);        }        printf("Scenario #%d:\n%d\n\n",flag++,spfa(1,n));    }    return 0;}

最大生成树

//prime-------266ms#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0X3f3f3f3fusing namespace std;typedef long long ll;const int MAXN=1e6+10;const int MAX=2000+10;int n,m,countt;int num,first[MAX];int book[MAX],dis[MAX];struct EDGE{    int v,w,next;}edge[MAXN*2];          //链式向前星void init(){    num=0;    countt=0;    fill(dis,dis+n+1,0);    memset(book,0,sizeof(book));    memset(first,-1,sizeof(first));}void addedge(int u,int v,int w){    edge[num].v=v;    edge[num].w=w;    edge[num].next=first[u];    first[u]=num++;}int prime(int s){    book[s]=1;    dis[s]=0;    countt++;    int ans=inf;    for(int i=first[s];i!=-1;i=edge[i].next){        dis[edge[i].v]=edge[i].w;    }    while(countt<n){        int maxx=0,t=0;        for(int i=1;i<=n;i++){            if(!book[i]&&maxx<dis[i]){                maxx=dis[i];                t=i;            }        }        ans=min(ans,maxx);        if(t==n)    break;        book[t]=1;        countt++;        for(int i=first[t];i!=-1;i=edge[i].next){            int v=edge[i].v;            int w=edge[i].w;            if(!book[v]&&w>dis[v]){                dis[v]=w;            }        }    }    return ans;}int main(){    #ifdef ONLINE_JUDGE    #else    freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    #endif    int T;    int flag=1;    cin>>T;    while(T--){        init();        cin>>n>>m;        int u,v,w;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w);            addedge(v,u,w);        }        printf("Scenario #%d:\n%d\n\n",flag++,prime(1));    }    return 0;}
//kruskal------219ms#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0X3f3f3f3fusing namespace std;typedef long long ll;const int MAXN=1e6+10;const int MAX=2000+10;//最小生成树Kruskalint n,m;int f[MAX];struct Edge{    int u,v,w;}edge[MAXN];void init(){    for(int i=1;i<=n;i++)        f[i]=i;}int find(int x){    return x==f[x]?x:f[x]=find(f[x]);   }int merge(int x,int y){    int xt=find(x),yt=find(y);    if(xt!=yt){        f[xt]=yt;        return 1;    }    return 0;}bool juge(int x,int y){    return find(x)==find(y);}int cmp(Edge a,Edge b){    return a.w>b.w;}int Kruskal(){    int ans=inf;    int countt=0;    for(int i=1;i<=m;i++){        if(!juge(edge[i].u,edge[i].v)){            merge(edge[i].u,edge[i].v);            countt++;            //sum+=edge[i].w;            ans=min(ans,edge[i].w);        }        if(juge(1,n))            break;    }    return ans;}int main(){    #ifdef ONLINE_JUDGE    #else    freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    #endif    int T;    int flag=1;    cin>>T;    while(T--){        cin>>n>>m;        init();        int u,v,w;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);        }        sort(edge+1,edge+m+1,cmp);        printf("Scenario #%d:\n%d\n\n",flag++,Kruskal());    }    return 0;}
原创粉丝点击