lightoj 1002 Country Roads (Dijkstra变形)

91 篇文章 1 订阅


1002 - Country Roads
Time Limit: 3 second(s)Memory Limit: 32 MB

I am going to my home. There are many cities and manybi-directional roads between them. The cities are numbered from 0 to n-1and each road has a cost. There are m roads. You are given the number ofmy city t where I belong. Now from each city you have to find theminimum cost to go to my city. The cost is defined by the cost of the maximumroad you have used to go to my city.

For example, in the abovepicture, 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 wecan 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 mlines, each will contain three integers u, v, w (0 ≤ u, v < n, u≠ v, 1 ≤ w ≤ 20000) indicating that there is a roadbetween u and v with cost w. Then there will be a singleinteger t (0 ≤ t < n). There can be multiple roads between twocities.

Output

For each case, print the case number first. Then for all thecities (from 0 to n-1) you have to print the cost. If there is nosuch 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

Note

Dataset is huge, user faster I/O methods.


题目链接:http://lightoj.com/volume_showproblem.php?problem=1002


题目大意:求某个点到各点的代价,两点间某条路径的代价定义为路径上权值最大的边的权值,而两点间的权值表示为它们之间所有路径代价的最小值


题目分析:Dijkstra的变形,dis[u]表示从原点到点u的路径的代价,最后注意判断重边


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int const MAX = 650;
int const INF = 0x3fffffff;

int n, m, t;

int dis[MAX], map[MAX][MAX];
bool vis[MAX];

void Dijkstra(int v0)
{
	for(int i = 0; i < n; i++)
		dis[i] = INF;
	for(int i = 0; i < n; i++)
		if(map[v0][i] != INF)
			dis[i] = map[v0][i];
	vis[v0] = true;
	dis[v0] = 0;
	for(int i = 0; i < n - 1; i++)
	{
		int u = v0, mi = INF;
		for(int j = 0; j < n; j++)
		{
			if(!vis[j] && dis[j] < mi)
			{
				mi = dis[j];
				u = j;
			}
		}
		vis[u] = true;
		for(int j = 0; j < n; j++)
		{
			if(!vis[j] && map[u][j] != INF)
			{
				int tmp = max(dis[u], map[u][j]);
				dis[j] = min(dis[j], tmp);
			}
		}
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	for(int ca = 1; ca <= T; ca++)
	{
		printf("Case %d:\n", ca);
		scanf("%d %d", &n, &m);
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
			{
				map[i][j] = INF;
				if(i == j)
					map[i][j] = 0;
			}
		for(int i = 0; i < m; i++)
		{
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			if(map[u][v] > w)
			{
				map[u][v] = w;
				map[v][u] = w;
			}
		}
		scanf("%d", &t);
		memset(vis, false, sizeof(vis));
		Dijkstra(t);
		for(int i = 0; i < n; i++)
			if(dis[i] != INF)
				printf("%d\n", dis[i]);
			else
				printf("Impossible\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值