Poj 1135 Domino Effect (Dijkstra)

Source: Internet
Author: User

Description:
Do you know that Domino is used for other purposes besides playing domino games? Dominoes game: get one
Some dominoes are lined up in a row, and there is only a short gap between the two dominoes. If it is well arranged, when you push
Pouring 1st dominoes will cause other dominoes to fall continuously (this is the origin of the phrase "domino effect ).
However, when the number of dominoes is small, this method is not very interesting, so some people created another one in the early 1980s S.
An extreme domino game: Uses millions of dominoes of different colors and materials to create a complex pattern. They created
Is a popular art. In this kind of dominoes, there are usually multiline dominoes that fall at the same time.
Your task is to write a program. Given such a dominoes game, you can calculate which dominoes fell down at the end
Time falls. These dominoes games contain some "key cards" that are connected by a line of common dominoes. When a key
When the card falls, all the lines connecting to the card start to fall. When the fallen row reaches another key card that has not yet fallen,
These key dominoes also began to fall, and all the lines connected to it also began to fall. Each line of the card can start from two ends
Any key card in the vertex starts to fall, and even the key cards of the two endpoints can fall separately. In this case, the line
The final fallen card is a bone card in the middle. Assuming that the card breaks down at the same speed.
Input description:
The input file contains multiple test data, each of which describes a domino game. The
1 behavior two integers: n and m, n represents the number of key cards, 1 ≤ n <500; m represents m rows of common bone between the n cards
Card connection. The number of n key cards is 1 ~ N. There is at most one normal line between each pair of key cards, and the domino card Pattern
Is connected, that is, from a card can be connected to each other card through a series of lines.
Next, there are m rows, each with three integers: a, B, and t, indicating that there is a line between the and B.
Normal card connection. It takes t seconds for this line to backward to the other end. Every domino game is a game that has been pushed down from 1st million cards
.
The last line of the input file is n = m = 0, indicating that the input is complete.
Output description:

For each test data in the input file, first output a line "System # k", where k is the serial number of the test data; then
Output a line, first the time when the last card fell, accurate to a valid number after the decimal point, and then finally fell
The position of the dominoes. This final card is either a key card or a common card between two cards. Output Grid
As shown in the sample output. If multiple solutions exist, any one is output. An empty line is output after the output of each test data.
Sample input:
2 1
1 2 27
3 3
1 2 5
1 3 5
2 3 5
0 0
System #1
The last domino falls after 27.0 seconds, at key domino 2.
Sample output:
System #2
The last domino falls after 7.5 seconds, between key dominoes 2 and 3.

It is a question of the Dijkstra template. It determines the optimal solution.
A) Calculate the dist [I] of each key card first. This requires the Dijkstra algorithm to calculate 1st cards for each other

The shortest path of the card. Set the maximum value of dist [I] To max1.

B) Calculate the time when each row falls completely. If the key cards at both ends of each row are set to I and j, when this row falls completely

(Dist [I] + dist [j] +)
Edge [I] [j])/2.0, where edge [I] [j] is the line connecting the I and j cards.

The time spent. Take the maximum value of the time when all rows completely fall, and set it to max2.

C) if max2> max1, it is the second case; otherwise, it is the first case.
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cmath>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Deprecision MAX 600
# Define INF 0x7FFFFFFF
# Define eps 1e-5
Using namespace std;
Int n, m;
Int edge [MAX] [MAX], visit [MAX], dist [MAX]; // edge information, access, shortest distance
 
Void dijkstra (int u0)
{
Int I, j, v;
For (I = 1; I <= n; I ++) // Initialization
{
Dist [I] = edge [u0] [I];
}
Visit [u0] = 1;
For (j = 0; j <n-1; j ++)
{
Int min = INF;
For (I = 1; I <= n; I ++)
{
If (! Visit [I] & min> dist [I])
{
Min = dist [I];
V = I;
}
}
Visit [v] = 1; // the same set of vertex v and Source Vertex
For (I = 1; I <= n; I ++) // update the dist value as the new vertex is added.
{
If (! Visit [I] & edge [v] [I] <INF & edge [v] [I] + dist [v] <dist [I])
Dist [I] = edge [v] [I] + dist [v];
}
}
}
 
Int main ()
{
Int I, j, a, B, c, tt = 1;
While (scanf ("% d", & n, & m ))
{
If (n = 0 & m = 0)
Break;
For (I = 1; I <= n; I ++) // Initialization
For (j = 1; j <= n; j ++)
{
If (I = j)
Edge [I] [j] = 0;
Else
Edge [I] [j] = INF;
}
For (I = 0; I <m; I ++) // Diagram
{
Scanf ("% d", & a, & B, & c );
Edge [a] [B] = c;
Edge [B] [a] = c;
}
Memset (visit, 0, sizeof (visit ));
Dijkstra (1); // obtain the shortest path from 1 to all specified points.

Double max1 =-10000000;
Int index;
For (I = 1; I <= n; I ++) //
{
If (max1 <dist [I])
{
Max1 = dist [I] * 1.0;
Index = I;
}
}

Double max2=-10000000;
Int index1, index2;
For (I = 1; I <= n; I ++) // case B
For (j = 1; j <= n; j ++)
{
If (edge [I] [j]! = INF & I <j) // I <j is an optimization. If yes, It is 0 ms, but not 16 ms.
{
If (max2 <(dist [I] + dist [j] + edge [I] [j])/2.0)
{
Max2 = (dist [I] + dist [j] + edge [I] [j])/2.0;
Index1 = I;
Index2 = j;
}
}
}

Printf ("System # % d \ n", tt ++ );
If (max1> = max2) // select the matching conditions
Printf ("The last domino falls after %. 1f seconds, at key domino % d. \ n", max1, index );
Else
Printf ("The last domino falls after %. 1f seconds, between key dominoes % d and % d. \ n", max2, index1, index2 );
Printf ("\ n ");
}
Return 0;
}

Related Article

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.