最大生成树POJ1797Heavy Transportation解题报告

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 25218 Accepted: 6677
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
将边从大到小排序。从最大边开始,依次取出一边e。如果边e两顶点都在生成树(不在同一集合),找下一边,否则将边e加到生成树上(将两顶点放到同一集合father[x]=y;)。
每次取加入生成树中的边长和ans中较小的。这样ans就是生成树中最小的边。
如何证明生成树中最小的边恰好就在从点1到点n的路径上呢?在找到答案之前(1和n在同一集合)要么1先进入生成树,要么n先进入。不失一般性,假设1先进入。那么一旦n进入生成树就不再加入边。由于边长时递减的,所以最后进入的含n的边的长度一定是所有生成树中最短的之一(可能含n的边的长度与前一条相等的)。 而一旦点n进入入生成树就表示点1和点n相连。所以,生成树中最短的边一定在点1到点n的路径上。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 1005
#define INF 0xffffff
using namespace std;
int father[MAXN];
struct Street{
    int x,y,w;
};
int findRoot(int x){//带路径压缩
    return father[x]==x?x:father[x]=findRoot(father[x]);
}
bool cmp(Street a,Street b){
    return a.w>b.w;
}
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int T,n,m;
    scanf("%d",&T);
    for(int i=1;i<=T;i++){
            int ans=INF;
            scanf("%d%d",&n,&m);
            Street *street=new Street[m+1];//题目没有给出m大小,用指针
            for(int i=1;i<=n;i++) father[i]=i;
            for(int i=0;i<m;i++)
                scanf("%d%d%d",&street[i].x,&street[i].y,&street[i].w);
            sort(street,street+m,cmp);
            for(int i=0;i<m;i++){
                int x=findRoot(street[i].x),y=findRoot(street[i].y);
                if(x!=y){//两点还不在同一集合
                    father[x]=y;//两点合并在用一集合,边放到生成树中
                    if(ans>street[i].w)
                        ans=street[i].w;
                    if(findRoot(1)==findRoot(n)) 
                        break;//一旦两点都在生成树中就完成
                }
            }
           printf("Scenario #%d:\n%d\n\n",i,ans);//真是要注意细节啊,两个换行符
        }
      return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值