POJ 1135 Domino Effect (Dijkstra shortest)

Source: Internet
Author: User


Domino Effect
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9335 Accepted: 2325

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.

Source

Southwestern European Regional Contest 1996

Title Link: http://poj.org/problem?id=1135

The main topic: There are n key dominoes, M road, from the beginning of a road to the end of the card all down need time t, calculate the last card down where, is when

Problem Analysis: Two cases:
1. The last card to be fallen is a key card, and the time is the maximum value in the shortest ma1
2. The last fallen card between a certain two cards, the time is two cards time plus two cards between the time of the fall, in addition to the maximum value of 2 MA2
Finally compare M1 and M2, if M1 large is the first case, otherwise it is the second case

A set of examples:
4 4
1 2 5
2 4 6
1 3 5
3 4 7
0 0

Answer:
The last domino falls after 11.5 seconds, between key dominoes 3 and 4.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const INF = 0x3fffffff  ; int const MAX = 505;int Map[max][max];int dis[max];bool used[max];int N, m, CA = 1;void Dijkstra (int v0) {memset (used,    False, sizeof (used));    for (int i = 1; I <= n; i++) dis[i] = Map[v0][i];    Used[v0] = true;    Dis[v0] = 0;        for (int i = 0; i < n-1; i++) {int u = 1, MI = INF;  for (int j = 1; J <= N; j + +) {if (!used[j] && dis[j] < mi) {mi =                DIS[J];            U = j;        }} Used[u] = true; for (int k = 1; k <= N; k++) if (!used[k] && map[u][k] < INF) Dis[k] = min (dis[k], D    Is[u] + map[u][k]);    } Double ma1 =-1, ma2 =-1;    int POS, POS1, Pos2;            for (int i = 1; I <= n; i++) {if (Dis[i] > ma1) {ma1 = dis[i];        pos = i; }} for (int i = 1; I <= N; i++) {for (int j = 1; J <= N; j + +) {if (Map[i][j] < INF && (Dis[i] + dis[j] + ma                P[I][J])/2.0 > Ma2) {ma2 = (Dis[i] + dis[j] + map[i][j])/2.0;                POS1 = i;            Pos2 = j; }}} if (Ma1 < MA2) printf ("The last Domino falls after%.1f seconds, between key dominoes%d and%    D.\n\n ", Ma2, POS1, Pos2);        else printf ("The last Domino falls after%.1f seconds, at key Domino%d.\n\n", MA1, POS);         }int Main () {while (scanf ("%d%d", &n, &m)! = EOF && (n + m)) {for (int i = 1; I <= n; i++)            {Dis[i] = INF;        for (int j = 1; J <= N; j + +) map[i][j] = INF;            } for (int i = 0; i < m; i++) {int u, V, W;            scanf ("%d%d%d", &u, &v, &w);            MAP[U][V] = W;        Map[v][u] = W; } printf ("System #%d\n", CA ++);    Dijkstra (1); }}


POJ 1135 Domino Effect (Dijkstra shortest)

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.