24h購物| | PChome| 登入
2013-06-09 06:48:19| 人氣1,992| 回應0 | 上一篇 | 下一篇

[UVA][spfa] 318 - Domino Effect

推薦 0 收藏 0 轉貼0 訂閱站台


 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 file contains descriptions of several domino systems. The first line of each description contains two integers: the number n of key dominoes ( tex2html_wrap_inline29 ) 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 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.


題目描述:
骨牌效應,給定 n 個點,點與點之間會有骨牌,如果單方面從 x 到 y 點,需要 v 秒,
即 x 倒了之後,需要 v 秒,y 才會倒。但如果 x 倒了之後,從 x 處往 y 處推,
在 v 秒內發生 y 也倒了,那麼會從兩個地方同時逼近到中間骨牌相會。

題目解法:

先做一次最短路,然後將所有邊都拿出來判斷一下中間骨牌相會的時間。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct E {
int to;
double v;
E(int a, double b):
to(a), v(b) {}
};
vector<E> g[505];
double dist[505];
void spfa(int st) {
int tn, inq[505] = {};
queue<int> Q;
dist[st] = 0;
Q.push(st);
while(!Q.empty()) {
tn = Q.front(), Q.pop();
inq[tn] = 0;
for(vector<E>::iterator it = g[tn].begin();
it != g[tn].end(); it++) {
if(dist[it->to] > dist[tn] + it->v) {
dist[it->to] = dist[tn] + it->v;
if(inq[it->to] == 0) {
inq[it->to] = 1;
Q.push(it->to);
}
}
}
}
}
int main() {
int n, m, cases = 0;
int i, j, x, y;
double v;
while(scanf("%d %d", &n, &m) == 2 && n) {
for(i = 1; i <= n; i++) {
g[i].clear();
dist[i] = 0xfffffff;
}
while(m--) {
scanf("%d %d %lf", &x, &y, &v);
g[x].push_back(E(y, v));
g[y].push_back(E(x, v));
}
spfa(1);
double single = 0, two = -1;
int sret = 1, tret1 = 1, tret2 = 1;
for(i = 1; i <= n; i++) {
if(dist[i] > single) {
single = dist[i];
sret = i;
}
}
for(i = 1; i <= n; i++) {
for(vector<E>::iterator it = g[i].begin();
it != g[i].end(); it++) {
if(dist[i] > dist[it->to]) {
double time = (it->v - (dist[i]-dist[it->to]))/2.0;
if(time + dist[i] > two) {
two = time + dist[i];
tret1 = i, tret2 = it->to;
}
} else {
double time = (it->v - (dist[it->to]-dist[i]))/2.0;
if(time + dist[it->to] > two) {
two = time + dist[it->to];
tret1 = i, tret2 = it->to;
}
}
}
}
if(tret1 > tret2) swap(tret1, tret2);
printf("System #%d\n", ++cases);
if(single+1e-8 > two) {
printf("The last domino falls after %.1lf seconds, at key domino %d.\n", single, sret);
} else {
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", two, tret1, tret2);
}
puts("");
}
return 0;
}

台長: Morris
人氣(1,992) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][搜索] 10605 - Mines For Diamonds
此分類上一篇:[UVA] 824 - Coast Tracker

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文