Domino Effect
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10482 Accepted: 2596
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
题意:
关于domino骨牌游戏,里面有n个关键骨牌,关键骨牌之间有一行普通骨牌。告诉你关键骨牌之间一行骨牌倒下所需要的时间。问你最后一个骨牌倒下的时间的位置。(两个相邻关键骨牌可以同时倒下,则中间骨牌在两个关键骨牌之后倒下)。
分析:
这题就是dijkstra算法的变形;dis[i]表示距离(时间)。
最后倒下的骨牌有两种情况:为关键骨牌或两个关键骨牌之间的普通骨牌。
以样例一为例:
如果骨牌是从1->2倒下的,则最后倒下的骨牌的时间为:(dis[1]+dis[2]+E[1][2])/2.0,并且这个结果等于dis[2];
如果是1-><-2,则最后倒下的骨牌的时间也为: (dis[1]+dis[2]+E[1][2])/2.0,但是这个结果小于dis[2];
所以,用max1记录dis[i]的最大值,max2记录(dis[i]+dis[j]+E[i][j])/2.0的最大值。如果max2>max1则为第二种情况,否则为第一种情况。
**。。。。。。**for循环里有一个i忘了初始化wa了好几次。。。
是无向边,所以E[u][v],E[v][u]要同时存。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int INF = 10000000;
int dis[maxn];
int E[maxn][maxn];
int s[maxn];
int n,m,cas;
void dijkstra()
{
memset(s,0,sizeof(s));
dis[1]=0;s[1]=1;
for(int i=2;i<=n;i++)
dis[i]=E[1][i];
for(int i=0;i<n-1;i++)
{
int minn=INF,u=0;
for(int j=1;j<=n;j++)
{
if(!s[j] && dis[j]<minn)
{
u=j;
minn=dis[j];
}
}
s[u]=1;
for(int j=1;j<=n;j++)
{
if(!s[j] && E[u][j]<INF && dis[j]>dis[u]+E[u][j])
dis[j]=dis[u]+E[u][j];
}
}
double max1=-INF,max2=-INF,t;
int pos,pos1,pos2;
for(int i=1;i<=n;i++)
{
if(dis[i]>max1)
{
max1=dis[i];
pos=i;
}
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
t=(dis[i]+dis[j]+E[i][j])/2.0;
if(E[i][j]<INF && t>max2)
{
max2=t;
pos1=i;
pos2=j;
}
}
}
printf("System #%d\n",cas++);
if(max2>max1)
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",max2,pos1,pos2);
else
printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",max1,pos);
}
int main()
{
cas=1;
int u,v,w;
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++)
{
E[i][j]=INF;
}
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
E[u][v]=w;
E[v][u]=w;
}
dijkstra();
}
return 0;
}