zoukankan      html  css  js  c++  java
  • POJ 1135 Domino Effect 最短路径+枚举

    Domino Effect
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5539   Accepted: 1396

    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 /* 功能Function Description:     最短路径  POJ-1135   迪杰斯特拉+枚举
     2    开发环境Environment:          DEV C++ 4.9.9.1
     3    技术特点Technique:
     4    版本Version:
     5    作者Author:                   可笑痴狂
     6    日期Date:                      20120807
     7    备注Notes:
     8    思路:
     9        1、达到每个关键多米诺的时间就是与第一个关键多米诺骨牌的最短距离
    10           与第一个关键多米诺骨牌距离最远的 关键 多米诺骨牌必定最晚倒下。---注意这里是 关键骨牌 中间的不是关键的骨牌可能最后倒下
    11        2、此题的解题方法还是相当巧妙地说。先求出源点到每个顶点的最短路径,然后对每一行的A,B两个顶点,
    12           如果(time[i]+time[j]+Edge[i][j])/2.0 大于 max{time[i] | i=1,2,...,n} ,那么就是在这行中间倒下了。
    13 */
    14 
    15 #include<stdio.h>
    16 #define Max 0x3fffffff
    17 int map[550][550];
    18 int dis[550];
    19 
    20 void dijkstra(int n)
    21 {
    22     int visit[505]={0};
    23     int min,i,j,k;
    24     visit[1]=1;
    25     for(i=1;i<n;++i)
    26     {
    27         min=Max;
    28         k=1;
    29         for(j=1;j<=n;++j)
    30         {
    31             if(!visit[j]&&min>dis[j])
    32             {
    33                 min=dis[j];
    34                 k=j;
    35             }
    36         }
    37         visit[k]=1;
    38         for(j=1;j<=n;++j)
    39         {
    40             if(!visit[j]&&dis[j]>dis[k]+map[k][j])
    41                 dis[j]=dis[k]+map[k][j];
    42         }
    43     }
    44 }
    45 
    46 int main()
    47 {
    48     int n,m,k1,k2,i,j,T=0;
    49     double max1,max2;
    50     int from[501],to[501],cost[501];
    51     while(scanf("%d%d",&n,&m),m||n)
    52     {
    53         if(n==1)          //注意只有一个的时候另外输出 
    54         {
    55             printf("System #%d\nThe last domino falls after 0.0 seconds, at key domino 1.\n\n",++T);
    56             continue;
    57         }    
    58         for(i=1;i<=n;++i)
    59         {
    60             map[i][i]=0;
    61             for(j=1;j<i;++j)
    62                 map[i][j]=map[j][i]=Max;
    63         }
    64         for(i=1;i<=m;++i)
    65         {
    66             scanf("%d%d%d",&from[i],&to[i],&cost[i]);
    67             map[from[i]][to[i]]=map[to[i]][from[i]]=cost[i];
    68         }
    69         for(i=1;i<=n;++i)
    70             dis[i]=map[1][i];
    71         dijkstra(n);
    72         max1=-1.0;
    73         for(i=1;i<=n;++i)
    74             if(max1<dis[i]*1.0)
    75             {
    76                 max1=dis[i]*1.0;
    77                 k1=i;
    78             }
    79         max2=-1.0;
    80         for(i=1;i<=m;++i)
    81             if(max2<(dis[from[i]]+dis[to[i]]+cost[i])*0.5)
    82             {
    83                 max2=(dis[from[i]]+dis[to[i]]+cost[i])*0.5;
    84                 k2=i;
    85             }
    86         if(max1<max2)  
    87             printf("System #%d\nThe last domino falls after %.1lf seconds, between key dominoes %d and %d.\n\n",++T,max2,from[k2],to[k2]);
    88         else
    89             printf("System #%d\nThe last domino falls after %.1lf seconds, at key domino %d.\n\n",++T,max1,k1);
    90     }
    91     return 0;
    92 }



    功不成,身已退
  • 相关阅读:
    vijos 1426
    2455 繁忙的都市
    2104 删除物品
    3235 战争
    BZOJ 2962
    COGS 265 线段覆盖
    P2184 贪婪大陆
    0729模拟赛解题报告
    BZOJ 1012
    BZOJ 2763
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2627217.html
Copyright © 2011-2022 走看看