Lightoj 1002 Country Roads (Dijkstra deformation), lightojdijkstra

Source: Internet
Author: User

Lightoj 1002 Country Roads (Dijkstra deformation), lightojdijkstra


1002-Country roanalyticdb
PDF (English) Statistics Forum
Time Limit: 3 second (s) Memory Limit: 32 MB

I am going to my home. There are using cities and manybi-directional roads between them. The cities are numbered from0ToN-1And each road has a cost. There areMRoads. You are given the number ofmy cityTWhere 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 integerT (≤ 20), Denoting the number of test cases.

Each case starts with a blank line and two integersN (1 ≤ n ≤ 500)AndM (0 ≤ m ≤16000). The nextMLines, each will contain three integersU, v, w (0 ≤ u, v <n, u = v, 1 ≤ w ≤ 20000)Indicating that there is a roadbetweenUAndVWith costW. Then there will be a singleintegerT (0 ≤ t <n). There can be multiple roads between twocities.

Output

For each case, print the case number first. Then for all thecities (from0ToN-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.


Question link: http://lightoj.com/volume_showproblem.php? Problem = 1002.


Calculate the cost from a point to each point. The cost of a path between two points is defined as the edge weight with the largest weight value on the path, the weights between two points represent the minimum cost of all paths between them.


Problem Analysis: Dijkstra deformation, dis [u] indicates the cost of the path from the origin point to the u, and finally pay attention to judge the heavy edge


#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]);elseprintf("Impossible\n");}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.