Poj 1135 Domino Effect

题目链接

Domino Effect
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12518   Accepted: 3144

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.

题意:给定1<=n<=500个关键牌多米诺骨牌,之间用普通骨牌连接,共m行。而且多米诺牌图案是连通的。推到第一块,输出最后一块倒下的时间。倒下的要么是关键牌,要么是关键牌之间的普通牌。

分析:如前所述,最后倒下牌的情形

  (1)倒下的是关键牌,就是到此节点的最短路;

  (2)普通牌倒下的时间为两张关键牌倒下时间加上这一行倒下所需要的时间。

本题具体步骤:

  (1)利用Dijkstra求出1号关键骨牌到其他关键骨牌所需的时间,然后取最大值max1,就是最后倒下的骨牌的时间;

  (2)然后计算关键牌i,j之间普通牌都倒下所需时间(dis[i]+dis[j]+e[i][j])/2.0,取最大值max2;

  (3)如果max1>=max2,就是第一种情况,否则是第二种

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1010;
const int inf=0x3f3f3f3f;
int e[N][N],dis[N];
bool vis[N];
int n,m;
void init(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j) e[i][j]=0;
            else e[i][j]=inf;
        }
    }
}
void Dij(int st){
    memset(vis, false, sizeof(vis));
    vis[st] = true;
    for(int i = 1; i <= n; i++){
        dis[i] = e[st][i];
    }
    for(int i = 1; i < n; i++){
        int minn = inf, u;
        for(int j = 1; j <= n; j++){
            if(dis[j] < minn && !vis[j]){
                minn = dis[j];
                u = j;
            }
        }
        vis[u] = true;
        for(int j = 1; j <= n; j++){
            if(dis[j] > dis[u] + e[u][j] && !vis[j]){
                dis[j] = dis[u] + e[u][j];
            }
        }
    }
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int a, b, c, iCase = 1;
    while(scanf("%d%d",&n,&m)){
        if(!n && !m) break;
        init();
        for(int i = 1;i <= m; i++){
            scanf("%d%d%d", &a, &b, &c);
            e[a][b] = e[b][a] = c;
        }
        Dij(1);
//        for(int i = 1; i <= n; i++){
//            printf("%d ", dis[i]);
//        }
//        printf("\n");
        printf("System #%d\n",iCase++);
        printf("The last domino falls after ");
        double max1 = -inf, max2 = -inf;
        int t1,t2,t3;
        for(int i = 1; i <= n; i++){
            if(max1 < dis[i]){
                max1 = dis[i]; t1 = i;
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j=i+1;j<=n;j++){
                double tt = (dis[i] + dis[j] + e[i][j]) / 2.0;
                if(max2 < tt && e[i][j] < inf){
                    max2 = tt; t2 = i; t3 = j;
                }
            }
        }
        if(max1 >= max2){
            printf("%.1lf seconds, at key domino %d.\n",max1,t1);
        }
        else{
            printf("%.1lf seconds, between key dominoes %d and %d.\n", max2, t2, t3);
        }
        printf("\n");
       // printf("%.1lf %.1lf\n", max1, max2);
    }
    return 0;
}

 

posted on 2018-10-08 12:50  坤sir  阅读(237)  评论(3编辑  收藏  举报

导航