1135 Domino Effect
来源:互联网 发布:h3c 无线mac地址认证 编辑:程序博客网 时间:2024/05/22 00:05
Domino Effect
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4646 Accepted: 1210
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.
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.
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.
Source
Southwestern European Regional Contest 1996
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define MAXV 505#define INF 1000000 //INF要足够大,由于开小了所以wa了一次int n,m;int a,b,t;int i,j;int dist[MAXV];int edges[MAXV][MAXV];double maxtime1,maxtime2;int key,key1,key2;int cas = 0;void Dijkstra(int v) { int s[MAXV]; int mindis,i,j,u = 0; for(i=0;i<n;i++) { dist[i] = edges[v][i]; s[i] = 0; } s[v] = 1; dist[v] = 0; for(i=0;i<n;i++){ mindis = INF; for(j=0;j<n;j++) if(s[j]==0&&dist[j]<mindis) { u = j; mindis = dist[j]; } s[u] = 1; for(j=0;j<n;j++) if(s[j]==0) if(edges[u][j]<INF&&dist[u]+edges[u][j]<dist[j]) dist[j] = dist[u]+ edges[u][j]; }}void solve_case() { maxtime1 = -1; for(i=0; i<n; i++) if(dist[i] > maxtime1 && dist[i] < INF) { maxtime1 = dist[i]; key = i; } maxtime2 = -1; for(i=0; i<n; i++) for(j=0; j<n; j++) { double temp = (double)(dist[i] + dist[j] + edges[i][j])/2; if(edges[i][j] < INF && temp > maxtime2) { maxtime2 = temp; key1 = i; key2 = j; } } if(maxtime1>=maxtime2 ){ printf("System #%d\n",++cas); printf("The last domino falls after %.1f seconds, at key domino %d.\n",maxtime1,key+1); printf("\n"); } else { printf("System #%d\n",++cas); printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",maxtime2,key1+1,key2+1); printf("\n"); }}int main() { while(cin >> n >> m) { if(n==0&&m==0) break; /*for(i=0; i<n; i++) for(j=0; j<n; j++) edges[i][j] = INF;*/ memset(edges,INF,sizeof(edges)); for(i=0; i<m; i++) { cin >> a >> b >> t; edges[a-1][b-1] = t; edges[b-1][a-1] = t; } Dijkstra(0); solve_case(); } return 0;}
- poj 1135 Domino Effect
- 1135 Domino Effect
- POJ 1135Domino Effect
- POJ-1135-Domino Effect
- poj 1135 Domino Effect
- poj 1135 Domino Effect
- POJ 1135 Domino Effect
- POJ-1135 Domino Effect
- poj 1135 Domino Effect
- poj 1135 Domino Effect
- Poj 1135 Domino Effect(Dijkstra)
- poj 1135 Domino Effect (Dijkstra)
- POJ 1135 Domino Effect (Dijkstra)
- POJ 1135 Domino Effect(Dijkstra)
- Domino Effect POJ - 1135 题解
- POJ 1135 Domino Effect 笔记
- [ZOJ 1298][POJ 1135] Domino Effect
- 最短路径 POJ 1135 Domino Effect
- 项目总结
- 几个著名Java开源缓存框架介绍
- c语言中time函数的用法
- spring mvc 上传文件400错误解决方法
- SQL获取所有数据库名、表名、储存过程以及参数列表
- 1135 Domino Effect
- ASP.NET后台注册javascript脚本方法
- MFC中播放WAV文件的方法
- VC++6.0中使用ADO方式操作ACCESS数据库(转载)
- python 中的if __name__ == 'main':
- Hibernate懒加载异常处理
- Linux 压缩解压 tar 命令与参数
- Temporary Objects
- 感情还是输给了时间