1002 - Country Roads
I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you have to find the minimum cost to go to my city. The cost is defined by the cost of the maximum road you have used to go to my city.
For example, in the above picture, if we want to go from 0 to 4, then we can choose
1) 0 - 1 - 4 which costs 8, as 8 (1 - 4) is the maximum road we used
2) 0 - 2 - 4 which costs 9, as 9 (0 - 2) is the maximum road we used
3) 0 - 3 - 4 which costs 7, as 7 (3 - 4) is the maximum road we used
So, our result is 7, as we can use 0 - 3 - 4.
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with a blank line and two integers n (1 ≤ n ≤ 500) and m (0 ≤ m ≤ 16000). The next m lines, each will contain three integers u, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 20000) indicating that there is a road between u and v with cost w. Then there will be a single integer t (0 ≤ t < n). There can be multiple roads between two cities.
For each case, print the case number first. Then for all the cities (from 0 to n-1) you have to print the cost. If there is no such path, print ‘Impossible‘.
Sample Input |
Output for Sample Input |
2
5 6 0 1 5 0 1 4 2 1 3 3 0 7 3 4 6 3 1 8 1
5 4 0 1 5 0 1 4 2 1 3 3 4 7 1 |
Case 1: 4 0 3 7 7 Case 2: 4 0 3 Impossible Impossible |
Dataset is huge, user faster I/O methods.
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<vector> 7 #include<string.h> 8 void prim(int n,int cnt); 9 int ma[600][600]; 10 using namespace std; 11 typedef long long LL; 12 typedef struct pp { 13 int from; 14 int to; 15 int cost; 16 } ss ; 17 int d[600]; 18 vector<ss>vec[600]; 19 int main(void) { 20 int i,j,k,p,q; 21 int x,y,z; 22 int s; 23 scanf("%d",&k); 24 for(s=1; s<=k; s++) { 25 for(i=0; i<600; i++) { 26 vec[i].clear(); 27 for(j=0; j<600; j++) { 28 ma[i][j]=1e9; 29 30 } 31 } 32 scanf("%d %d",&p,&q); 33 while(q--) { 34 scanf("%d %d %d",&x,&y,&z); 35 if(ma[x][y]>z) { 36 ma[y][x]=z;ma[x][y]=z; 37 } 38 } 39 for(i=0; i<600; i++) { 40 for(j=0; j<600; j++) { 41 if(ma[i][j]!=1e9) { 42 ss dd; 43 dd.cost=ma[i][j]; 44 dd.from=i; 45 dd.to=j; 46 vec[dd.from].push_back(dd); 47 } 48 } 49 } 50 int u; 51 cin>>u; 52 prim(u,p-1); 53 printf("Case %d:\n",s); 54 for(i=0; i<p; i++) 55 if(d[i]!=1e9)printf("%d\n",d[i]); 56 else printf("Impossible\n"); 57 } 58 return 0; 59 60 } 61 void prim(int n,int cnt) { 62 fill(d,d+600,1e9); 63 d[n]=0; 64 queue<int>que;bool flag[600]; 65 memset(flag,0,sizeof(flag)); 66 while(true) 67 { 68 int l=-1;int i; 69 for(i=0;i<=cnt;i++) 70 { 71 if((l==-1||d[i]<d[l])&&!flag[i]) 72 { 73 l=i; 74 } 75 } 76 if(l==-1||d[l]==1e9) 77 { 78 break; 79 } 80 flag[l]=true; 81 for(i=0;i<vec[l].size();i++) 82 { 83 if(d[vec[l][i].to]>vec[l][i].cost) 84 { 85 d[vec[l][i].to]=max(vec[l][i].cost,d[l]); 86 } 87 } 88 } 89 }
1002 - Country Roads(light oj)
原文:http://www.cnblogs.com/zzuli2sjy/p/5285919.html