Domino Effect
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10325 |
|
Accepted: 2560 |
Description
Did you know the can use domino bones for other things besides playing dominoes? Take a number of dominoes and build a row by standing them on end with only a small distance in between. If you did it right, you can tip the first domino and cause all others to fall down in succession (this is where the phrase "Domino effect" comes from).
While this is somewhat pointless with only a few dominoes, some people went to the opposite extreme in the early eighties. Using millions of dominoes of different colors and materials to fill whole halls with elaborate patterns of falling domin OES, they created (short-lived) pieces of art. In these constructions, usually isn't only one but several rows of dominoes were falling at the same time. As you can imagine, timing is a essential factor here.
It is now your task to write a program this, given such a system of rows formed by dominoes, computes when and where the L AST Domino Falls. The system consists of several ' key dominoes ' connected by rows of simple dominoes. When a key domino falls, all rows connected to the domino would also start falling (except for the ones that has already f Allen). When the falling rows reach other key dominoes that has not fallen yet, these other key dominoes would fall as well and SE T off the rows connected to them. Domino rows may start collapsing at either end. It is even possible that a row was collapsing on both ends, in which case the last domino falling in this row is somewhere Between its key dominoes. You can assume this rows fall at a uniform rate.
Input
The input file contains descriptions of several domino systems. The first line of each description contains the integers:the number N of key dominoes (1 <= N <) and the number m of rows between them. The key dominoes is numbered from 1 to N. There are at the very one row between any pair of key dominoes and the domino graph is connected, i.e. there are at least one WA Y-to-get from a domino-to-any-domino by following a series of domino rows.
The following m lines each contain three integers a, B, and L, stating this there is a row between key dominoes A and b th At takes l seconds to fall down from end to end.
Each system was started by tipping over key Domino number 1.
The file ends with a empty system (with n = m = 0), which should isn't be processed.
Output
For each case output a line stating the number of the case (' System #1 ', ' System #2 ', etc.). Then output a line containing the time when the last domino falls, exact to one digit to the right of the decimal point, a nd the location of the last domino falling, which are either at a key domino or between-key dominoes (in this case, OUTP UT the numbers in ascending order). Adhere to the format shown in the output sample. The test data would ensure there is only one solution. Output a blank line after each system.
Sample Input
2 11 2 273 31 2 51 3 52 3 50 0
Sample Output
System #1The last Domino falls after 27.0 seconds in key Domino 2.System #2The last Domino falls after 7.5 seconds, betwe En key Dominoes 2 and 3.
Title Link: http://poj.org/problem?id=1135
The title means: The input n,m indicates that there are n key cards, m indicates that there is a M-line between the n cards to connect the common card. The number of n cards is 1~n. Each of the two key cards is only a single line of ordinary cards, and the pattern is connected. Push from the 1th key card. The last down if it is a key card, the output to look at the sample, if it is a normal card, output to look at the example.
Idea: Because the starting point is fixed, can be used to find the shortest path of the Dijkastra algorithm (also can use BFS search, if less time on into row). Find out the fall time of each key card. Two key card i,j the ordinary card between the total down time is (Edge[i][j]+time[i]+time[j]) *1.0/2.0, if the time is not equal to time[i] or time[j], time is the last time this line of ordinary cards fall down Otherwise, the last card to fall is the key card;
Code:
#include <iostream>#include<cstdio>using namespacestd;#defineINF 10000000intn,m;intedge[510][510];intsign[510],time[510];voidInit () {inti,j; intu,v,w; for(i=0; i<=n; i++) {Time[i]=INF; Sign[i]=0; for(j=0; j<=n; J + +) Edge[i][j]=INF; } for(i=0; i<m; i++) {scanf ("%d%d%d",&u,&v,&W); EDGE[U][V]=edge[v][u]=W; }}voidDijkstra (intv0) { inti,j,t; Time[v0]=0; for(i=0; i<n; i++) {Sign[v0]=1; intmin=INF; for(j=1; j<=n; J + +) { if(edge[v0][j]<inf&&time[v0]+edge[v0][j]<Time[j]) time[j]=time[v0]+Edge[v0][j]; if(sign[j]==0&&time[j]<Min) {Min=Time[j]; T=J; }} v0=T; if(v0<=0&&v0>n) Break; }}intMain () {inti,j; intt=1; while(SCANF ("%d%d", &n,&m) &&! (n==0&&m==0) ) {Init (); Dijkstra (1); floatmax1=0; intflag=1; for(i=1; i<=n; i++) if(time[i]>Max1) {Max1=Time[i]; Flag=i; } floatMax2=0; intA=1, b=1; for(i=1; i<=n; i++) { for(j=1; j<=n; J + +) if(edge[i][j]<INF) { floatAns= (Edge[i][j]+time[i]+time[j]) *1.0/2; if(ans>Max2) {MAX2=ans; A=i; b=J; } }} cout<<"System #"<<t<<Endl; if(MAX2>MAX1) printf ("the last domino falls after%.1f seconds, between key dominoes%d and%d.\n\n", max2,a,b); Elseprintf"the last domino falls after%.1f seconds, at key Domino%d.\n\n", Max1,flag); T++; } return 0;}
View Code
POJ 1135.Domino Effect