zoukankan      html  css  js  c++  java
  • zoj1298 poj1135

    Domino Effect

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 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 input 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. Adhere to the format shown in the output sample. If you find several solutions, output only one of them. 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.

     

     

    #include <cstdio>
    #include <cstdlib>
    #define MAXN 500
    #define INF 1000000 
    int n, m; 
    int Edge[MAXN][MAXN]; 
    int caseno = 1; 
    int time[MAXN];
    int S[MAXN]; 
    void solve_case( )
    {
        int i, j, k; 
        for( i=0; i<n; i++ )
        {
            time[i] = Edge[0][i];  S[i]=0;  
        }
        time[0] = 0;  S[0] = 1;
        for( i=0; i<n-1; i++ ) 
        {
            int min = INF, u = 0;  
            for( j=0; j<n; j++ ) 
            {   
                if( !S[j] && time[j]<min )    
                {    
                    u=j;
                    min = time[j];  
                }
            }   
            S[u] = 1;  
            for( k=0; k<n; k++ )  
            {   
                if( !S[k] && Edge[u][k]<INF && time[u] + Edge[u][k] < time[k] )  
                {    
                    time[k] = time[u] + Edge[u][k];  
                }   
            }
        }
        double maxtime1 = -INF;  int pos; 
        for( i=0; i<n; i++ )
        {  
            if( time[i]>maxtime1 )
            {   
                maxtime1 = time[i]; 
                pos = i;  
            }
        }
        double maxtime2 = -INF, t;
        int pos1, pos2;  
        for( i=0; i<n; i++ )
        {  
            for( j=0; j<n; j++ )
            {
                t = (time[i]+time[j] + Edge[i][j])/2.0;  
                if( Edge[i][j]<INF &&  t > maxtime2 )
                {   
                    maxtime2 = t;  pos1 = i;  pos2 = j;  
                }   
            }
        }
        printf( "System #%d\n", caseno++ ); 
        printf( "The last domino falls after " );
        if( maxtime2>maxtime1 )  
            printf( "%.1f seconds, between key dominoes %d and %d.\n\n",    maxtime2, pos1 + 1, pos2 + 1 );
        else
            printf( "%.1f seconds, at key domino %d.\n\n", maxtime1, pos + 1 );
    }
    int read_case( ) 
    {
        int i, j; 
        int v1, v2, t; 
        scanf( "%d %d", &n, &m );
        if( n == 0 && m == 0 ) 
            return 0;
        for( i=0; i<n; i++ )  
        {  
            for( j=0; j<n; j++ ) 
                Edge[i][j] = INF; 
        }
        for( i=0; i<m; i++ )
        {  
            scanf( "%d %d %d", &v1, &v2, &t);
            v1--;  v2--;  
            Edge[v1][v2] = Edge[v2][v1] = t;
        }
        return 1;
    }
    int main( )
    {
        while( read_case( ) )  
            solve_case( );
        return 0;
    }
  • 相关阅读:
    时间戳计算
    .NET/C#/Oracle数据库操作类
    memcached安装及.NET中的Memcached.ClientLibrary使用
    C# Redis
    Jmeter教程 简单的压力测试
    Elasticsearch查询类型
    Windows 如何查看本地端口被进程占用的情况?
    Log4net 配置
    NLog 配置
    RPC原理
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3013149.html
Copyright © 2011-2022 走看看