Heavy Transportation

来源:互联网 发布:图像阈值分割算法研究 编辑:程序博客网 时间:2024/06/07 02:23

描述

BackgroundHugo 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.ProblemYou 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.

输入

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.

输出

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.

样例输入

13 31 2 31 3 42 3 5

样例输出

Scenario #1:4

题目大意

有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量找一条1-->N的路径,在不超过每条路径的最大载重量的情况下,使得运送的货物最多。

这道题不就是最大生成树吗。。为什么大家的题解都奇奇怪怪。orz。
显然,我们按边权排序,然后从大到小添加,直到使得1,n在同一个集合,就break;
可以用最短路算法、搜索等方法做,但不推荐

Notice:注意输出2个回车否则会Presentation Error

//#include<bits/stdc++.h>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int T,n,m;#define N 100010 struct Edge{    int from,to,w;    bool operator < (const Edge _t)const{        return this->w>_t.w;    }}e[N];int fa[N];int find(int u){    return fa[u]==u?fa[u]:fa[u]=find(fa[u]);}int main(){    int u,v,w,fx,fy;    scanf("%d",&T);    for(int i=1;i<=T;i++){        scanf("%d%d",&n,&m);        for(int j=1;j<=m;j++){            scanf("%d%d%d",&e[j].from,&e[j].to,&e[j].w);        }        sort(e+1,e+1+m);        for(int j=1;j<=n;j++)fa[j]=j;        int ans = 0x7fffffff;        for(int j=1;j<=m;j++)        {            fx = find(e[j].from);            fy = find(e[j].to);            if(fx!=fy){                fa[fx]=fy;                if(find(1)==find(n)){                    ans = e[j].w;                    break;                }            }                   }        printf("Scenario #%d:\n%d\n\n",i,ans);    }       return 0;}
1 0
原创粉丝点击