POJ 1135 Domino Effect (Shortest path)

Source: Internet
Author: User

Test instructions
There is a domino of M emissions, each row of the head of the tail bone card called "Key Domino", a total of n key dominoes, each row of Domino fall need l seconds, the key dominoes fall off the time negligible. Push down the key Domino 1 to find the time and position for the last domino to fall. (If the last fallen domino is not the key domino, then output the key dominoes at both ends of the row where the domino is located in ascending order)

Sample input
2 1
1 2 27
3 3
1 2 5
1 3 5
2 3 5
0 0

Sample output
System #1
The last domino falls after 27.0 seconds, at Key Domino 2.

System #2
The last domino falls after 7.5 seconds, between key Dominoes 2 and 3.

Ideas
The Dijkstra algorithm is used to find the shortest path from the key Domino 1 to each key domino (that is, the shortest time-consuming). And the moment when the side that is irrelevant to the shortest path falls, is the sum of the shortest time required for the key domino of the key Domino 1 to the end of the edge, plus the time required for the edge to fall, divided by 2. (Understanding this is the key to this problem, similar and encounter problems) Finally, as long as the key Domino 1 to a critical domino of the shortest path time-consuming and a side of the fall of the moment, take the larger side of the two, you can draw a conclusion.

Watch out.
Since the critical domino fall time is negligible, when the input data is only 1 dominoes, time is 0.0 and the critical domino of the fall is 1 per se.

1#include <cstdio>2#include <cstdlib>3#include <string.h>4#include <queue>5 using namespacestd;6 7 Const intN =510, INF =0x3f3f3f3f;8 intGraph[n][n];9 Ten voidSPFA (intN) One { Aqueue<int>Q; -     BOOLVis[n] = {0}; -vis[0] =true; the     intD[n]; -      for(intI=0; i<n; ++i) -     { -D[i] = graph[0][i]; +         if(graph[0][i]! =INF) -         { + Q.push (i); AVis[i] =true; at         } -     } -      while(!q.empty ()) -     { -         intx =Q.front (); -          for(intI=0; i<n; ++i) in         { -             intdis = d[x] +Graph[x][i]; to             if(Dis <D[i]) +             { -D[i] =dis; the                 if(!Vis[i]) *                 { $ Q.push (i);Panax NotoginsengVis[i] =true; -                 } the             } +         } A Q.pop (); theVIS[X] =false; +     } -d[0] =0; $     intMax1 =0, MAX2 =0, Sum, Tag1, Tag21, tag22; $      for(intI=1; i<n; ++i) -         if(D[i] >Max1) -Max1 = D[i], Tag1 =i; the      for(intI=0; i<n; ++i) -     {Wuyi          for(intj=i+1; j<n; ++j) the         { -             if(Graph[i][j]! =INF) Wusum = D[i] + d[j] +Graph[i][j]; -             if(Sum >Max2) AboutMAX2 = SUM, Tag21 = i, tag22 =J; $         } -     } -     DoubleAns = (Double) MAX2/2; -     if(Ans <= (Double) Max1) Aprintf"the last domino falls after%.1lf seconds, at key Domino%d.\n\n", (Double) Max1, tag1+1); +     Else theprintf"the last domino falls after%.1lf seconds, between key dominoes%d and%d.\n\n", ans, tag21+1, tag22+1); -     return ; $ } the  the intMainvoid) the { the     intN, M, U, V, W; -      for(intt=0; scanf"%d%d", &n, &m), n+m;) in     { thememset (graph, INF,sizeof(graph)); the          for(intI=0; i<m; ++i) About         { thescanf" %d%d%d", &u, &v, &W); the--u,--v; theGRAPH[U][V] =W; +Graph[v][u] =W; -         } theprintf"System #%d\n", ++t);Bayi         if(n = =1) theprintf"the last domino falls after 0.0 seconds, at key Domino 1.\n\n"); the         Else - SPFA (n); -     } the     return 0; the}

Test data

2 11 2  -3 31 2 51 3 52 3 51 05 7   1 2 11 3 21 4 31 5 52 3 13 4 14 5 14 51 2 31 3 31 4 53 4 22 3 93 31 3 11 2 32 3 30 0
inch
System #1The last Domino Falls after27.0Seconds, at Key Domino2. System #2The last Domino Falls after7.5Seconds, between key dominoes2and3. System #3The last Domino Falls after0.0Seconds, at Key Domino1. System #4The last Domino Falls after4.5Seconds, between key dominoes1and5. System #5The last Domino Falls after7.5Seconds, between key dominoes2and3. System #6The last Domino Falls after3.5Seconds, between key dominoes2and3.
out

POJ 1135 Domino Effect (Shortest path)

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.