Domino Effect
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 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 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 |
分析:
最后倒下的牌 有两种情形:
① 后倒下的牌是关键牌,其时间及位置就是第 1 张关键牌到其他关键牌中短路径的大值及对应的关键牌;
② 后倒下的牌是两张关键牌之间的某张普通牌,其时间为这两张 关键牌倒下时间的一半再加上及这一行倒下时间的一半,位置为这两张牌之间的某张普通牌
那就变为最短路+枚举了。
代码:
#include<cstdio> #include<cstring> #define maxn 505 #define INF 0x3f3f3f3f using namespace std ; int n,m,ans; bool vis[maxn]; int dist[maxn],city[maxn][maxn]; void init() { int i,j; memset(dist,0x3f,sizeof(dist)); memset(city,0x3f,sizeof(city)); memset(vis,0,sizeof(vis)); } void disjk() { int i,j,mi,k,now=1; vis[1]=1; dist[1]=0; for(i=1;i<n;i++) { for(j=1;j<=n;j++) { if(!vis[j]&&city[now][j]+dist[now]<dist[j]) dist[j]=city[now][j]+dist[now]; } mi=INF; for(j=1;j<=n;j++) { if(!vis[j]&&dist[j]<mi) { mi=dist[j]; k=j; } } now=k; vis[k]=1; } } int main() { int i,j,t=0,le,ri,d,e,e1,e2; double ma,temp; while(scanf("%d%d",&n,&m)&&!(n==0&&m==0)) { t++; init(); for(i=1;i<=m;i++) { scanf("%d%d%d",&le,&ri,&d); city[le][ri]=city[ri][le]=d; } disjk(); // 求最短路 ma=-1; for(i=1;i<=n;i++) // 第一种情况 { if(dist[i]>ma) { ma=dist[i]; e=i; } } e1=-1; for(i=1;i<=n;i++) // 第二种情况 { for(j=i+1;j<=n;j++) { if(city[i][j]!=INF) { temp=(dist[i]+dist[j]+city[i][j])*1.0/2.0; if(ma<temp) { e1=i; e2=j; ma=temp; } } } } printf("System #%d\n",t); if(e1==-1) printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",ma,e); else printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",ma,e1,e2); } return 0; }