POJ-1135 Domino Effect

Domino Effect
Time Limit: 1000MS Memory Limit: 65536K

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 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 (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 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(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 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.
————————————————————集训11.2的分割线————————————————————

思路:稍微思考之后,发现既然计算的是时间,那么每一张关键牌肯定是在最短路径上倒下的。因此跑一遍Dijkstra。注意!这是无向图。之后在所有的最短路径当中找到最长的路径,可能就是答案。但是有可能最后一张倒下的骨牌不是关键牌。

这就需要通过关键牌的最短路径求出每一行骨牌全部倒下花费的时间。其值应该是

(关键牌i倒下+关键牌j倒下+该行倒下)/ 2

因为这是两个路径都算了一遍,两个路径又是同时开始的,所以取中间值就是真正花费的时间

假如有任意一行倒下的时间比刚才的答案大,最后一张牌就不是关键牌。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define INF 0x7f7f7f7f
using namespace std;
/****************************************/
const int N = 505;
int G[N][N], dis[N], vis[N], n, m;

bool cmp(int a, int b)
{
	return a > b;
}

void Dijkstra()
{
	memset(vis, 0, sizeof(vis));
	memset(dis, 0x7f, sizeof(dis));
	dis[1] = 0;
	int cur;
	for(int i = 1; i < n; i++) {
		int mini = INF;
		for(int k = 1; k <= n; k++) if(!vis[k]) {
			if(mini > dis[k]) {
				mini = dis[k];
				cur = k;
			}
		}
		vis[cur] = true;
		for(int k = 1; k <= n; k++) if(!vis[k]) {
			dis[k] = min(dis[k], dis[cur] + G[cur][k]);
		}
	}
}

int main()
{
#ifdef J_Sure
	freopen("222.in", "r", stdin);
//  freopen(".out", "w", stdout);
#endif
	int cas = 1;
	while(scanf("%d%d", &n, &m)!=EOF&&n) {
		int u, v, w;
		memset(G, 0x7F, sizeof(G));
		for(int i = 0; i < m; i++) {
			scanf("%d%d%d", &u, &v, &w);
			G[u][v] = G[v][u] = w;//这里WA很久
		}
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				if(i == j)
					G[i][j] = 0;
			}
		}
		Dijkstra();
		int SP = -1, ansI, maxI, maxJ;
		double maxT = -1;
		for(int i = 1; i <= n; i++) {
			if(dis[i] > SP) {
				SP = dis[i];
				ansI = i;
			}
			for(int j = 1; j <= n; j++) {
				if(G[i][j] == INF) continue;
				double a = (dis[i] + dis[j] + G[i][j])*1.0/2;
				if(a > maxT) {
					maxT = a;
					maxI = i;
					maxJ = j;
				}
			}
		}
		if(SP < maxT)
			printf("System #%d\nThe last domino falls after %.1f seconds, between key dominoes %d and %d.\n", cas++, maxT, maxI, maxJ);
		else
			printf("System #%d\nThe last domino falls after %d.0 seconds, at key domino %d.\n", cas++, SP, ansI);
		puts("");
	}
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值