lightoj 1002 Country Roads (Dijkstra變形),lightojdijkstra

來源:互聯網
上載者:User

lightoj 1002 Country Roads (Dijkstra變形),lightojdijkstra


1002 - Country Roads
PDF (English) Statistics Forum
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 from0 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 integersn (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 roadbetweenu and v with cost w. 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 (from0 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]);elseprintf("Impossible\n");}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.