zoukankan      html  css  js  c++  java
  • POJ 1135.Domino Effect Dijkastra算法

    Domino Effect

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10325   Accepted: 2560

    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.

    题目链接:http://poj.org/problem?id=1135


    题目意思:输入n,m表示有n张关键牌,m表示n张牌之间有m行普通牌进行连接。n张牌的编号为1~n。每两张关键牌之间之多只有一行普通牌,并且图案是联通的。从第1张关键牌推到。最后倒下的如果是关键牌,输出看样例;如果是普通牌,输出看样例。

    思路:因为起点固定,可以用求最短路径的Dijkastra算法(也可以用BFS搜索,如果时间更少就入列)。求出每张关键牌的倒下时间。两个关键牌i,j之间的普通牌完全倒下的时间为(edge[i][j]+time[i]+time[j])*1.0/2.0;如果时间不是等于time[i]或者time[j],时间就是这行普通牌最后倒下的时间;否则最后倒下的牌就是关键牌;


    代码:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define INF 10000000
    int n,m;
    int edge[510][510];
    int sign[510],time[510];
    void Init()
    {
        int i,j;
        int u,v,w;
        for(i=0; i<=n; i++)
        {
            time[i]=INF;
            sign[i]=0;
            for(j=0; j<=n; j++)
                edge[i][j]=INF;
        }
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            edge[u][v]=edge[v][u]=w;
        }
    }
    void Dijkstra(int v0)
    {
        int i,j,t;
        time[v0]=0;
        for(i=0; i<n; i++)
        {
            sign[v0]=1;
            int Min=INF;
            for(j=1; j<=n; j++)
            {
                if(edge[v0][j]<INF&&time[v0]+edge[v0][j]<time[j])
                    time[j]=time[v0]+edge[v0][j];
                if(sign[j]==0&&time[j]<Min)
                {
                    Min=time[j];
                    t=j;
                }
            }
            v0=t;
            if(v0<=0&&v0>n) break;
        }
    }
    int main()
    {
        int i,j;
        int t=1;
        while(scanf("%d%d",&n,&m)&&!(n==0&&m==0))
        {
            Init();
            Dijkstra(1);
            float Max1=0;
            int flag=1;
            for(i=1; i<=n; i++)
                if(time[i]>Max1)
                {
                    Max1=time[i];
                    flag=i;
                }
            float Max2=0;
            int a=1,b=1;
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=n; j++)
                    if(edge[i][j]<INF)
                    {
                        float ans=(edge[i][j]+time[i]+time[j])*1.0/2;
                        if(ans>Max2)
                        {
                            Max2=ans;
                            a=i;
                            b=j;
                        }
                    }
            }
            cout<<"System #"<<t<<endl;
            if(Max2>Max1) printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.
    
    ",Max2,a,b);
            else printf("The last domino falls after %.1f seconds, at key domino %d.
    
    ",Max1,flag);
            t++;
        }
        return 0;
    }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    Caused by: java.lang.ClassNotFoundException: org.w3c.dom.ElementTraversal
    Linux系统下jar包的启动方式
    1062 Error 'Duplicate entry '1438019' for key 'PRIMARY'' on query
    Linux学习笔记 --iptables防火墙配置
    MySQL学习笔记_10_MySQL高级操作(下)
    MySQL学习笔记_9_MySQL高级操作(上)
    MySQL学习笔记_8_SQL语言基础复习
    Linux 学习笔记_12_文件共享服务_4_SSH
    Linux 学习笔记_12_文件共享服务_3_NFS网络文件服务
    MySQL学习笔记_7_MySQL常用内置函数
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5418725.html
Copyright © 2011-2022 走看看