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.
InputInput 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.
OutputFor 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 Input2
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 OutputCase 1:
4
0
3
7
7
Case 2:
4
0
3
Impossible
Impossible
Note题意:某人要回家,求每一点 到 家T 的路径上最大的一条边权。
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int mm[1005][1005],vis[1005],dis[1005],n,m;
void dijkstra(int s)
{
int i,j,mi,v;
memset(vis,0,sizeof(vis));
for(i=0; i<n; ++i)
{
dis[i]=mm[s][i];
}
dis[s]=0;
vis[s]=1;
for(i=0; i<n; ++i)
{
mi=INF;
for(j=0; j<n; ++j)
{
if(!vis[j]&&dis[j]<mi)
{
mi=dis[j];
v=j;
}
}
vis[v]=1;
for(j=0; j<n; ++j)
{
if(!vis[j]&&mm[v][j]<INF) //求路径上权值最大,存入vis[];
{
int tmp=max(dis[v],mm[v][j]);
dis[j]=min(dis[j],tmp);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int cas=1; cas<=t; ++cas)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
mm[i][j]=INF;
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(mm[u][v]>w) //有重边,取最小。
{
mm[u][v]=w;
mm[v][u]=w;
}
}
int s;
scanf("%d",&s);
dijkstra(s);
printf("Case %d:\n",cas);
for(int i=0; i<n; ++i)
{
if(dis[i]<INF)
printf("%d\n",dis[i]);
else
printf("Impossible\n");
}
}
return 0;
}