Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8926 | Accepted: 2232 |
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.
这道题目就是n张多米诺骨牌,两两之间至多有一个连接(也是由骨牌组成,但是与这n张骨牌有区别),并且保证图联通。从骨牌1开始推到,问最后一张骨牌倒下位置。
显然结果有两种情况,一种是最后一张骨牌正好到在这n张骨牌的某张骨牌上,另一种就是倒在连接两张骨牌的中间的某个位置,我们来考虑第i张骨牌和第j张骨牌中间的骨牌全部倒下的时间。我们设骨牌正倒在i位置,所用时间为ti,倒在j位置所用时间为tj,并且i到j需要x时间。那么我们可以得到第i张骨牌和第j张骨牌中间的骨牌全部倒下的时间为(ti+tj+x)/2,为什么呢?
分两种情况讨论,这里假设ti<=tj(否则交换i,j坐标)
情况一:从骨牌1倒向j,中间经过了ij这条边,也就是1倒向i后从ij这条边倒向j,那么显然有ti+x=tj,并且可以发现第i张骨牌和第j张骨牌中间的骨牌全部倒下的时间为tj,此时将ti+x=tj带入(ti+tj+x)/2刚好也得到了tj,说明情况一成立。
情况二:从骨牌1倒向j,中间没有经过了ij这条边,那么因为起点都在1,时间又相同,因此两个方向走过的路程相同,而总路程是ti+tj+x,因此除以2,也就是(ti+tj+x)/2。
有了上面的讨论,这道题就很简单了,我们可以用dijkstra算法得到1到其余各点的最短路,并求得最短路中的最大值max1,然后暴力i,j计算(ti+tj+x)/2,同样用max2来保存这个最大值。之后比较max1和max2即可确定最后倒在什么位置。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 510
using namespace std;
int adj[Maxn][Maxn],vis[Maxn],dist[Maxn];
const int inf=0x3f3f3f3f;
void dijkstra(int u,int n){
for(int i=1;i<=n;i++)
dist[i]=adj[u][i],vis[i]=0;
vis[u]=1,dist[u]=0;
for(int i=1;i<n;i++){
int minn=1<<30,v;
for(int j=1;j<=n;j++)
if(!vis[j]&&dist[j]<minn)
minn=dist[j],v=j;
vis[v]=1;
for(int j=1;j<=n;j++)
if(!vis[j]&&dist[v]+adj[v][j]<dist[j])
dist[j]=dist[v]+adj[v][j];
}
}
int main()
{
int n,m,fr,to,w,cas=1;
while(scanf("%d%d",&n,&m),n){
memset(adj,0x3f,sizeof adj);
for(int i=0;i<m;i++){
scanf("%d%d%d",&fr,&to,&w);
adj[fr][to]=adj[to][fr]=w;
}
dijkstra(1,n);
int max1=-1,max2=-1,pos1,pos2,pos3;
for(int i=1;i<=n;i++)
if(2*dist[i]>max1)
max1=2*dist[i],pos3=i;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(adj[i][j]!=inf&&dist[i]+dist[j]+adj[i][j]>max2){
max2=dist[i]+dist[j]+adj[i][j];
pos1=i,pos2=j;
}
printf("System #%d\nThe last domino falls after ",cas++);
if(max1>=max2) printf("%.1f seconds, at key domino %d.\n\n",max1/2.0,pos3);
else printf("%.1f seconds, between key dominoes %d and %d.\n\n",max2/2.0,pos1,pos2);
}
return 0;
}