POJ - 1797 Heavy Transportation

来源:互联网 发布:网吧公告软件 编辑:程序博客网 时间:2024/05/19 13:17
Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u

Submit Status

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

13 31 2 31 3 42 3 5

Sample Output

Scenario #1:4

自己的想法是求‘最大’生成树,边是按从大到小排序的,最后加入的就是最小的边,既为题目所求,结果也差不多是这样实现的,还是不知道为什么wrong了几次。貌似最短路的算法也可以ac出来,以后再试试。

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int maxn = 1000 + 10;const int maxm = 1000 * 1000 + 10;struct Edge{int u, v, dis;bool operator<(const Edge &b)const{return dis >b.dis;}}e[maxm];int n, m;int F[maxn];int find(int x){return F[x] == -1 ? x : F[x] = find(F[x]); }int kruskal(){memset(F, -1, sizeof(F));sort(e, e + m);//for (int i = 0; i < m; i++) //cout << e[i].u << " " << e[i].v << " " << e[i].dis << endl;for (int i = 0; i<m; i++){int u = e[i].u, v = e[i].v;if (find(u) != find(v)){F[find(u)] = find(v);if (find(1) == find(n)) return e[i].dis;}}return -1;}int main(){int T; scanf("%d", &T);for (int k = 1; k <= T; k++){scanf("%d%d", &n, &m);int jishu = 0;for (int i = 0; i < m;i++){scanf("%d%d%d", &e[jishu].u, &e[jishu].v, &e[jishu].dis);jishu++;}int ans=kruskal();printf("Scenario #%d:\n%d\n\n", k, ans);}return 0;}


0 0