Description
I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from0 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
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) andm (0 ≤ m ≤ 16000). The next m lines, each will contain three integersu, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 20000) indicating that there is a road betweenu and v with cost w. Then there will be a single integert (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 <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int Map[550][550];
bool vis[550];
int dis[550];
int n,m;
void Dijkstra(int t)
{
memset(dis,0,sizeof(dis));
memset(vis,false,sizeof(vis));
for(int i=0; i<n; i++)
dis[i] = Map[t][i];
vis[t] = true;
int pos;
while(1)
{
int M = INF;
for(int i=0; i<n; i++)
{
if(!vis[i] && M > dis[i])
{
M = dis[i];
pos = i;
}
}
if(M == INF)
break;
vis[pos] = true;
for(int i=0; i<n; i++)
{
if(!vis[i] && dis[i] > max(dis[pos],Map[pos][i]))
dis[i] = max(dis[pos],Map[pos][i]);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
int Case = 0;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i!=j)
Map[i][j] = INF;
else
Map[i][j] = 0;
}
}
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
if(Map[u][v] > w)
{
Map[u][v] = Map[v][u] = w;
}
}
int t;
cin>>t;
Dijkstra(t);
printf("Case %d:\n",++Case);
for(int i=0; i<n; i++)
{
if(dis[i] != INF)
printf("%d\n",dis[i]);
else
printf("Impossible\n");
}
}
return 0;
}