POJ 1135 Domino Effect

Domino Effect
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6486   Accepted: 1641

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

 

 

算是模版Dijkstra题目了,多了判断哪种最优解的情况。
a) 先计算每一张关键牌倒下的dist[i]。这需要利用Dijkstra 算法求第1 张关键牌到其他每

张关键牌的最短路径。然后取dist[i]的最大值,设为max1。

b) 计算每一行完全倒下的时间。设每一行的两端的关键牌为i 和j,则这一行完全倒下的时

间为(dist[i] + dist[j] +
edge[i][j])/2.0,其中edge[i][j]为连接第i、j 两张关键牌的行倒下

所花的时间。取所有行完全倒下时间的最大值,设为max2。

c) 如果max2 > max1,则是第②种情形;否则是第①种情形。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 const int INF=0x3f3f3f3f;
 8 const int maxn=520;
 9 
10 int n,m;
11 int map[maxn][maxn],vis[maxn],dis[maxn];
12 
13 void Dijkstra(int src){
14     int i,j,k;
15     for(i=1;i<=n;i++)
16         dis[i]=map[src][i];
17     memset(vis,0,sizeof(vis));
18     dis[src]=0;
19     vis[src]=1;
20     int tmp;
21     for(i=1;i<n;i++){
22         tmp=INF;
23         for(j=1;j<=n;j++)
24             if(!vis[j] && tmp>dis[j]){
25                 tmp=dis[j];
26                 k=j;
27             }
28         if(tmp==INF)
29             break;
30         vis[k]=1;
31         for(j=1;j<=n;j++)
32             if(!vis[j] && dis[j]>dis[k]+map[k][j])
33                 dis[j]=dis[k]+map[k][j];
34     }
35 }
36 
37 int main(){
38 
39     //freopen("input.txt","r",stdin);
40 
41     int cases=0;
42     while(~scanf("%d%d",&n,&m)){
43         if(n==0 && m==0)
44             break;
45         int i,j;
46         for(i=1;i<=n;i++)
47             for(j=1;j<=n;j++)
48                 map[i][j]=(i==j)?0:INF;
49         int a,b,c;
50         for(i=0;i<m;i++){
51             scanf("%d%d%d",&a,&b,&c);
52             map[a][b]=map[b][a]=c;
53         }
54         Dijkstra(1);
55         double max1=-INF;
56         int id;
57         for(i=1;i<=n;i++)
58             if(max1<dis[i]){
59                 max1=dis[i];
60                 id=i;
61             }
62         double max2=-INF;
63         int id1,id2;
64         for(i=1;i<=n;i++)
65             for(j=i+1;j<=n;j++)
66                 if(map[i][j]!=INF){
67                     if(max2<(dis[i]+dis[j]+map[i][j])/2.0){
68                         max2=(dis[i]+dis[j]+map[i][j])/2.0;
69                         id1=i;
70                         id2=j;
71                     }
72                 }
73         printf("System #%d\n",++cases);
74         if(max1>=max2)
75             printf("The last domino falls after %.1lf seconds, at key domino %d.\n",max1,id);
76         else
77             printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",max2,id1,id2);
78         printf("\n");
79     }
80     return 0;
81 }

 

你可能感兴趣的:(effect)