Country Roads LightOJ 1002(最短路变形)

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

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.

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

Note

Dataset is huge, user faster I/O methods.


好像打了一年了结果发现自己还是这么渣明天就要国赛了加油

最短路 不过这个最短路是要找到每一条路中最大的值想要这个值尽可能的小

DIj变形不同的是

if(dis[j] > max (maze[u][j],dis[u]))
{
	dis[j] = max(maze[u][j],dis[u]); 
}

代码:

#include<bits/stdc++.h>
using namespace std;
int maze[505][505];
int book[505],dis[505],n,m;
const int inf = 0x3f3f3f3f;
//单源最短路 
void Dij(int k)
{
	for(int i = 0;i < n;i ++)
		dis[i] = maze[k][i];//表示的是去各个地方的 
	for(int i = 0;i< n;i++)
		book[i] = 0;
	book[k] = 1;//这个东西去自己的 
	for(int i = 0;i<n;i++)
	{
		int u = -1;
		int maxn = inf;
		for(int j = 0;j < n;j++)
		{
			if(book[j]==0 && dis[j]< maxn)
			{
				maxn = dis[j];
				u = j;
			}
		}
		book[u] = 1;
		if(maxn == inf) break;
		//这个想要做的是找到每一条路中最大的那条路的最小值 
		for(int j = 0;j < n;j ++)
		{ 
			if(dis[j] > max (maze[u][j],dis[u]))
			{
				dis[j] = max(maze[u][j],dis[u]); 
			}
		}
	}	
	for(int i = 0;i<n;i++)
	{
		if(dis[i]!=inf)printf("%d\n",dis[i]);
		else printf("Impossible\n");
	}
	return ; 
}
int main()
{
	int t,x,y,w;
	scanf("%d",&t);
	for(int o = 1;o<=t;o++)
	{
		scanf("%d%d",&n,&m);
		for(int i = 0;i<=n;i++)
		{
			for(int j = 0;j<=n;j++)
				maze[i][j]= inf;
			maze[i][i] = 0;
		}	
		for(int i = 1;i <= m;i++)
		{
			scanf("%d%d%d",&x,&y,&w);
			if(maze[x][y]>w)
			{
				maze[x][y] = w;
				maze[y][x] = w;	
			}//这边要注意考虑重边的情况 
		}
		scanf("%d",&x);
		printf("Case %d:\n",o);
		Dij(x);
	}
 	return 0;
} 

猜你喜欢

转载自blog.csdn.net/galesaur_wcy/article/details/80434756