Domino Effect
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10230 |
|
Accepted: 2537 |
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.
Source
Southwestern European Regional Contest 1996
这道题看懂题目很重要;
简单的说就是,把位于1位置的推倒,问最后倒下的在哪里?需要多少时间?
如果最后一张倒下的位于关键点上,那就很好做;
但由于多米诺骨牌连续的,所以最后一张牌位于两个关键点之间,不妨记为A点和B点;
最后的时间是到A点的时间加上到B点的时间再加上A点到B点时间的一半;
为什么呢?
因为到达最终点时,实际上是两股骨牌的会和(从A点来的和从B点来的);
然后问题就简单了;
用最短路算法,求出1到各个点的最短路;
然后分情况一和情况二进行分别求出最大值,进行比较;
注意情况二中,并不是每两点间都存在通路的,不要忘记判断一下;
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue>
#include<iomanip>
#include<map>
#include<set>
#include<functional>
#define pi 3.14159265358979323846
using namespace std;
const int inf=0x3f3f3f3f;
int edge[501][501];
bool vis[501];
int dis[501];
int n,m;
void dijkstra()
{
dis[1]=0;
for(int i=2;i<=n;++i)
dis[i]=edge[1][i];
for(int i=0;i<n;++i)
{
int x,y=inf;
for(int j=1;j<=n;++j)
{
if(!vis[j]&&dis[j]<=y)
y=dis[x=j];
}
vis[x]=1;
for(int j=1;j<=n;++j)
{
if(!vis[j]&&dis[j]>dis[x]+edge[x][j])
dis[j]=dis[x]+edge[x][j];
//dis[j]=min(dis[j],dis[x]+edge[x][j]);
}
}
}
int main()
{
int cnt=0;
while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
{
++cnt;
if(n==1)
{
{
printf("System #%d\nThe last domino falls after 0.0 seconds, at key domino 1.\n\n",cnt);
continue;
}
}
double max1=0,max2=0;
int flag=0,mark1=0,mark2=0;
int a,b,l;
memset(vis,0,sizeof(vis));
memset(edge,inf,sizeof(edge));
for(int i=0;i<m;++i)
{
scanf("%d %d %d",&a,&b,&l);
edge[a][b]=l;
edge[b][a]=l;
}
dijkstra();
//cout<<dis[2]<<" "<<dis[3]<<" "<<dis[4]<<endl;
for(int i=2;i<=n;++i)
{
if(dis[i]>max1) {max1=dis[i];flag=i;}
}
double temp;
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
{
if(edge[i][j]!=inf)
{
temp=(dis[i]+dis[j]+edge[i][j])*0.5;
if(temp>max2)
{
max2=temp;
mark1=i;
mark2=j;
}
}
}
printf("System #%d\n",cnt);
if(max1>=max2)
{
printf("The last domino falls after %.1lf seconds, at key domino %d.\n\n",max1,flag);
}
else
{
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n\n",max2,mark1,mark2);
}
}
return 0;
}