Domino Effect
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 halls 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 (except 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 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 input ends with an empty system (with n = m = 0), which should 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. Adhere to the format shown in the output sample. If you find several solutions, output only one of them. Output a blank line after each system.
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.
题目类型:单源最短路
算法分析:题目所求的最后倒下的牌分两种情况:1最后倒下的牌是关键牌,所用时间是从源点1开始求得的最短路径中最大值。2最后倒下的牌是普通牌,所用时间是某一边邻接的两个节点的最短路径值和该边权之和的一半。推导过程:假设有dis[i]、dis[j]和edge[i][j] < INF。其中存在dis[i] < dis[j],则该边倒下的时间是dis[j] + [edge[i][j] – (dis[j] – dis[i])] / 2。化简为(dis[i] + dis[j] + edge[i][j]) / 2。最后求得上述的两个时间,比较大小并输出相应情况的解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 666 + 66; const int INF = 66666666; const int EPS = 1e-6; int edge[maxn][maxn]; int dis[maxn]; bool is_visited[maxn]; int n, m, flag = 1; void Dijkstra () { int i; for (i = 1; i <= n; i++) dis[i] = edge[1][i]; memset (is_visited, false, sizeof (is_visited)); is_visited[1] = true;//important!!!!!! dis[1] = 0; //important!!!!!! for (i = 2; i <= n; i++) { int minval = INF, minval_index; int j; for (j = 2; j <= n; j++) { if (!is_visited[j] && dis[j] < minval) { minval = dis[j]; minval_index = j; } } is_visited[minval_index] = true; for (j = 2; j <= n; j++) { if (!is_visited[j] && edge[minval_index][j] != INF && dis[j] > dis[minval_index] + edge[minval_index][j]) { dis[j] = dis[minval_index] + edge[minval_index][j]; } } } double maxtime1 = -INF; int index1; for (i = 1; i <= n; i++) { if (maxtime1 < dis[i]) { maxtime1 = dis[i]; index1 = i; } } double maxtime2 = -INF; int index_x, index_y; for (i = 1; i <= n; i++) { int j; for (j = 1; j <= n; j++) { double temp = (dis[i] + dis[j] + edge[i][j]) / 2.0; if (edge[i][j] < INF && maxtime2 < temp) { maxtime2 = temp; index_x = i; index_y = j; } } } if (maxtime2 > maxtime1) cout << "The last domino falls after " << fixed << setprecision (1) << maxtime2 << " seconds, between key dominoes " << index_x << " and " << index_y << "." << endl; else cout << "The last domino falls after " << fixed << setprecision (1) << maxtime1 << " seconds, at key domino " << index1 << "." << endl; } int main() { // ifstream cin ("aaa.txt"); while (cin >> n >> m) { if (n == 0 && m == 0) break; int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) edge[i][j] = INF; for (i = 0; i < m; i++) { int temp_a, temp_b, temp_val; cin >> temp_a >> temp_b >> temp_val; edge[temp_a][temp_b] = edge[temp_b][temp_a] = temp_val; } cout << "System #" << flag++ << endl; Dijkstra (); cout << endl; } return 0; } |