poj1797-Heavy Transportation(最大生成树)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/82998903

Heavy Transportation

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 50315   Accepted: 12988

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

Source

TUD Programming Contest 2004, Darmstadt, Germany

题意:

就是找1-n的所有路径中的最短边中的最大值。

这样就觉得好复杂。但是换一种想法,就是那我就找最长路,最长路上的最短边一定就是答案了。

一下就变成了最大生成树裸题了,注意的地方就是1和n一旦联通了就输出最后那条边就可以了。

也可以用dijkstra来做。

AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int maxn=1000010;
int cnt;
int fa[maxn],head[maxn];
struct node{
    int u;
    int v,next,val;
}G[maxn];
void init(){
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(fa,-1,sizeof(fa));
    ///memeset(G,0,sizeof(G));
}
void add(int a,int b,int c){
    G[++cnt].v=b;
    G[cnt].u=a;
    G[cnt].val=c;
    G[cnt].next=head[a];
    head[a]=cnt;
}
int findset(int x){return fa[x]==-1?x:fa[x]=findset(fa[x]);}
void join(int x,int y)
{
    int dx = findset(x), dy = findset(y);
    fa[dx] = dy;
}
int cmp(node a,node b){
    return a.val>b.val;
}
int main()
{
    int t;
    int n,m,a,b,c,ans;
    scanf("%d",&t);
    int cas=0;
    while(t--){
        cas++;
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        sort(G+1,G+1+m,cmp);
        ans=-1;
        for(int i=1;i<=m;i++){
            int u=G[i].u;
            int v=G[i].v;
            if(findset(u)!=findset(v)){
                join(u,v);
            }
            if(findset(1)==findset(n)){
                ans=G[i].val;
                break;
            }
        }
        printf("Scenario #%d:\n%d\n",cas,ans);
        printf("\n");

    }
    return 0;
}
/*
1
3 3
1 2 3
1 3 4
2 3 5

*/

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/82998903