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.
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
题意:有n个十字路口,每个十字路口之间只有一条街道,每个街道都有限重,货车的重量不能超过限重,但你要选一条能承装最大重量的通路从1到n。输出有两个换行。
思路:由于十字路口较多,因此不能用Floyd算法,其他的都可以,我这里用的是Dijkstra算法,不过要注意题意,求的是通路中最小起重中的最大值,与一般的Dijkstra算法相比,有一点改变。
代码
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 //ma用来存放两个点之间的距离 22 int ma[1010][1010]; 23 //dis用来存放到1之间的距离 24 int dis[1010]; 25 //vis数组标记已经找过的最大起重 26 bool vis[1010]; 27 28 void dijkstra(int m) 29 { 30 memset(vis,0,sizeof(vis)); 31 //记录从1到其他点的起重 32 for(int i=1;i<=m;i++) 33 { 34 dis[i]=ma[1][i]; 35 } 36 dis[1]=0; 37 for(int i=1;i<=m;i++) 38 { 39 int mi=-1; 40 int k=1; 41 //找所有点中最大的起重 42 for(int j=1;j<=m;j++) 43 { 44 if(!vis[j]&&mi<dis[j]) 45 { 46 mi=dis[j]; 47 k=j; 48 } 49 } 50 vis[k]=1; 51 for(int j=1;j<=m;j++) 52 { 53 if(!vis[j]) 54 { 55 //所有通路中最小起重中的最大值 56 dis[j]=max(dis[j],min(dis[k],ma[k][j])); 57 } 58 } 59 } 60 } 61 62 int main() 63 { 64 int ans=1,t; 65 scanf("%d",&t); 66 while(t--) 67 { 68 int m,n; 69 scanf("%d %d",&m,&n); 70 int a,b,c; 71 //初始化ma数组为0,表示两点之间的起重为0,不能运行 72 memset(ma,0,sizeof(ma)); 73 for(int i=1;i<=n;i++) 74 { 75 scanf("%d %d %d",&a,&b,&c); 76 ma[a][b]=ma[b][a]=c; 77 } 78 dijkstra(m); 79 printf("Scenario #%d: %d ",ans++,dis[m]); 80 } 81 }