Poj 1135 domino effect (implicit Shortest Path)

Source: Internet
Author: User
Domino Effect
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7164   Accepted:1813

Description

Did you know that you 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 do 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 Hils with elaborate patterns of falling dominoes, they created
(short-lived) pieces of art. in these constructions, usually not only one but several rows of dominoes were falling at the same time. as you can imagine, timing is an essential factor here.

It is now your task to write a program that, given such a system of rows formed by dominoes, computes when and where the last 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 will also start falling (could t for the ones that have already fallen ). when the falling rows reach other key dominoes that have not fallen yet, these other key dominoes will fall as well and set off the rows connected to them. domino
Rows may start collapsing at either end. it is even possible that a row is collapsing on both ends, in which case the last domino falling in that row is somewhere between its key dominoes. you can assume that rows fall at a uniform rate.

Input

The input file contains descriptions of several Domino systems. the first line of each description contains two integers: The number n of key dominoes (1 <= n <500) and the number M of rows between them. the key dominoes are numbered from 1 to n. there is
At most one row between any pair of key dominoes and the domino graph is connected, I. e. there is at least one way to get from a domino to any other Domino by following a series of Domino rows.

The following M lines each contain three integers A, B, and l, stating that there is a row between key dominoes A and B that takes L seconds to fall down from end to end.

Each system is started by tipping over key Domino number 1.

The file ends with an empty system (with N = m = 0), which shocould not 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, and the location of the last domino falling,
Which is either at a key domino or between two key dominoes (in this case, output the two numbers in ascending order ). adhere to the format shown in the output sample. the test data will 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, at key Domino 2. System # 2The last domino falls after 7.5 seconds, between key dominoes 2 and 3.

Source

Southwestern European Regional Contest 1996

Analysis:

There are two scenarios for the final card to fall:

(1) the cards that fall behind are the key cards. The time and position of the cards are the secrets of the short path from 1st million cards to other key cards and the corresponding key cards;

② The cards that fell down later were a common card between the two key cards. The time was half of the time when the two cards fell down, plus half of the time when the two key cards fell down, the location is a normal card between the two cards

Then it becomes the Shortest Path + enumeration.


Code:

# Include <cstdio> # include <cstring> # define maxn 505 # define INF 0x3f3f3fusing namespace STD; int n, m, ans; bool vis [maxn]; int Dist [maxn], city [maxn] [maxn]; void Init () {int I, j; memset (Dist, 0x3f, sizeof (DIST); memset (city, 0x3f, sizeof (city); memset (VIS, 0, sizeof (VIS);} void disjk () {int I, j, mi, K, now = 1; vis [1] = 1; Dist [1] = 0; for (I = 1; I <n; I ++) {for (j = 1; j <= N; j ++) {If (! Vis [J] & City [now] [J] + dist [now] <Dist [J]) dist [J] = City [now] [J] + dist [now];} MI = inf; For (j = 1; j <= N; j ++) {If (! Vis [J] & Dist [J] <mi) {mi = DIST [J]; k = J ;}now = K; vis [k] = 1 ;}} int main () {int I, j, T = 0, le, RI, D, E, E1, E2; double MA, temp; while (scanf ("% d", & N, & M )&&! (N = 0 & M = 0) {T ++; Init (); for (I = 1; I <= m; I ++) {scanf ("% d", & Le, & Ri, & D ); city [le] [ri] = City [ri] [le] = D;} disjk (); // calculate the Shortest Path MA =-1; for (I = 1; I <= N; I ++) // The first case {If (Dist [I]> Ma) {MA = DIST [I]; E = I ;}} e1 =-1; for (I = 1; I <= N; I ++) // The second case {for (j = I + 1; j <= N; j ++) {If (City [I] [J]! = Inf) {temp = (Dist [I] + dist [J] + city [I] [J]) * 1.0/2.0; If (MA <temp) {e1 = I; E2 = J; MA = temp ;}}} printf ("system # % d \ n", T); If (E1 =-1) printf ("the last domino falls after %. 1f seconds, at key Domino % d. \ n ", Ma, e); else printf (" the last domino falls after %. 1f seconds, between key dominoes % d and % d. \ n ", Ma, E1, E2);} 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.