C - 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.
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.
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.
1
3 3
1 2 3
1 3 4
2 3 5
Scenario #1:
4
思路: 题意是要求 路径上的边权的 最小值的最大值,, 就是说一个路径中最小的那边,一定要比别的路径的大; 可以最大的值来枚举,直到 find(1)==find(n) 即 起点和终点是联通的就行了
代码
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #define inf 0x3f3f3f #define M 10000000 using namespace std; struct data { int l,r,cost; }; data a[M]; int pre[M]; int n,m; void init() { int i; for(i=0;i<=n+100;i++) pre[i]=i; } int find(int x) { int r=x; // 刚开始忘记给r赋值,,找半天错,,简单的模版还是老错,真是!! if(r==pre[r]) return x; while(r!=pre[r]) r=pre[r]; int j=x; while(j!=r) { int k=pre[j]; pre[j]=r; j=k; } return r; } void join(int x,int y) { int xx=find(x); int yy=find(y); if(xx!=yy) pre[xx]=yy; } int cmp(data a,data b) { return a.cost>b.cost; } int ans=1; int main() { int t; scanf("%d",&t); while(t--) { int i,j; scanf("%d%d",&n,&m); init(); for(i=0;i<m;i++) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].cost); sort(a,a+m,cmp); for(i=0;i<m;i++) { if(find(a[i].l)!=find(a[i].r)) join(a[i].l,a[i].r); if(find(1)==find(n)) break; } printf("Scenario #%d:\n%d\n\n",ans++,a[i].cost); } }