LightOJ1002 --- Country Roads (transient deformation)

Source: Internet
Author: User

LightOJ1002 --- Country Roads (transient deformation)

I am going to my home. there are required cities and required 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

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.

Dijkstra algorithm Deformation

/*************************************** * *********************************> File Name: lightOJ1002.cpp> Author: ALex> Email: zchao1995@gmail.com> Created Time: tuesday ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; static const int N = 555; int dist [N]; priority_queue <PLL, vector
                
                  , Greater
                 
                   > Qu; struct node {int nxt; int w; int to;} edge [40000]; bool vis [555]; int head [N], tot; void addedge (int from, int to, int w) {edge [tot]. to = to; edge [tot]. w = w; edge [tot]. nxt = head [from]; head [from] = tot ++;} void Dijkstra (int u, int n) {while (! Qu. empty () {qu. pop () ;}for (int I = 0; I <n; ++ I) {dist [I] = inf;} dist [u] = 0; qu. push (make_pair (dist [u], u); while (! Qu. empty () {PLL tmp = qu. top (); qu. pop (); int s = tmp. second; int d = tmp. first; for (int I = head [s]; ~ I; I = edge [I]. nxt) {int t = edge [I]. to; int w = edge [I]. w; if (dist [t]> max (d, w) {dist [t] = max (d, w); qu. push (make_pair (dist [t], t) ;}}} int main () {int t, icase = 1; scanf ("% d", & t ); while (t --) {int n, m; scanf ("% d", & n, & m); for (int I = 0; I <n; ++ I) {head [I] =-1 ;}tot = 0; int u, v, w; for (int I = 1; I <= m; ++ I) {scanf ("% d", & u, & v, & w); addedge (u, v, w); addedge (v, u, w) ;}scanf ("% d", & u); Dijkstra (u, n); printf ("Case % d: \ n", icase ++ ); for (int I = 0; I <n; ++ I) {if (dist [I] = inf) {printf ("Impossible \ n ");} else {printf ("% d \ n", dist [I]) ;}} return 0 ;}
                 
                
               
              
             
            
          
         
        
       
      
     
    
   
  

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.