Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4393 | Accepted: 1139 |
Description
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 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
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.
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#define maxn 0x7fffff
using namespace std;
int n,m;
int dist[600];
int map[600][600];
int mark[600];
void dijkstra()
{
int i,j,min1,minj,temp;
dist[1]=0; //把起点并入集合,搜索时就可以从起点寻找到第一条最短的边了
for (i=1;i<=n-1;i++)
{
min1=maxn;
for (j=1;j<=n;j++) //查找到原集合的最短的边
{
if (!mark[j]&&dist[j]<min1)
{
min1=dist[j];
minj=j;
}
}
mark[minj]=1;
for (j=1;j<=n;j++) //每并入一个点都要对原来的边进行修正,保证任意时刻源点到目标点的距离都是最短的。
{
if (!mark[j]&&map[minj][j]>0)
{
temp=dist[minj]+map[minj][j];
if (temp<dist[j])
dist[j]=temp;
}
}
}
}
int main()
{
int t;
t=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=maxn;
for (int i=1;i<=n;i++) dist[i]=maxn;
memset(mark,0,sizeof(mark));
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
dijkstra();
double ans1=-maxn;
double ans2=-maxn;
int qi;
int wi,wj;
for(int i=1;i<=n;i++)
{
if(ans1<dist[i])
{
ans1=dist[i];
qi=i;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(map[i][j]<maxn&&ans2<(dist[i]+dist[j]+map[i][j])*1.0/2)
{
ans2=(dist[i]+dist[j]+map[i][j])*1.0/2;
wi=i;
wj=j;
}
}
printf("System #%d\n",t++);
if(ans1>=ans2)
printf("The last domino falls after %.1lf seconds, at key domino %d.\n",ans1,qi);
else
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",ans2,wi,wj);
cout<<endl;
}
}