标签:
Description
I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0to 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
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 uand v with cost w. Then there will be a single integer t (0 ≤ t < n). There can be multiple roads between two cities.
Output
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
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
Sample Output
Case 1:
4
0
3
7
7
Case 2:
4
0
3
Impossible
Impossible
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; int ans[10000]; int bin[10000]; struct node { int x,y,v; } p[20000]; int pos; int cmp(node p1,node p2) { return p1.v<p2.v; } int Find(int x) { return bin[x]==x?x:bin[x]=Find(bin[x]); } int n,m,s[1000]; int Kruskal() //最小生成树 { for(int i=0; i<n; i++) bin[i]=i; int num=0; for(int i=0; i<m; i++) { int f1=Find(bin[p[i].x]); int f2=Find(bin[p[i].y]); if(f1!=f2) { bin[f2]=f1; //num++; } f1=Find(bin[pos]); for(int j=0; j<n; j++) { if(j==pos||ans[j]) continue; f2=Find(bin[j]); if(f1==f2) ans[j]=p[i].v; } //if(num==n-1) //break; } } int main() { int T;int icase=0; scanf("%d",&T); while(T--) { memset(ans,0,sizeof(ans)); scanf("%d%d",&n,&m); for(int i=0; i<m; i++) { scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].v); } cin>>pos; sort(p,p+m,cmp); Kruskal(); printf("Case %d:\n",++icase); //此题因为icase总是初始化为一而错了好久 for(int i=0; i<n; i++) { if(i==pos) { puts("0"); continue; } if(!ans[i]) puts("Impossible"); else printf("%d\n",ans[i]); } } }
#include<iostream> #include<cstdio> #include<cstring> #define INF 0x3f3f3f3f using namespace std; int Edge[501][501]; int low[501]; int next[502]; int n,sum,m; int a[501]; int Prim(int u0) { int i,j; for(i=0;i<n;i++) { low[i]=Edge[u0][i]; next[i]=u0; } next[u0]=-1; int max=0; for(i=0;i<n-1;i++) { int min=INF; int v=-1; for(j=0;j<n;j++) { if(next[j]!=-1&&low[j]<min) { min=low[j]; v=j; } } if(min>max) max=min; //本体的核心 if(!a[v]) // a[v]=max; if(v!=-1) { //printf("%d %d %d\n",next[v],v,low[v]); //sum+=low[v]; next[v]=-1; for(j=0;j<n;j++) { if(next[j]!=-1 &&Edge[v][j]<low[j]) { low[j]=Edge[v][j]; next[j]=v; } } } } //printf("%d\n",sum); } int main() { int T; scanf("%d",&T); for(int kk=1;kk<=T;kk++) { sum=0; scanf("%d%d",&n,&m); memset(a,0,sizeof(a)); memset(Edge,INF,sizeof(Edge)); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(Edge[v][u]>w) Edge[u][v]=Edge[v][u]=w; } for(int i=0;i<n; i++) Edge[i][i]=0; int pos; scanf("%d",&pos); printf("Case %d:\n",kk); Prim(pos); for(int i=0;i<n;i++) { if( i!=pos) { if(a[i]) printf("%d\n",a[i]); else puts("Impossible"); } else puts("0"); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/became_a_wolf/article/details/47380271