zoukankan      html  css  js  c++  java
  • zoj 1298 Domino Effect (最短路径)

    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.


    Source: Southwest Europe 1996

    有点老也是不错的一道题。

     1 //Accepted    1298    C++    0    2168    姜伯约
     2 /*
     3     题意:
     4          从1开始推牌,问多少时间后牌全倒,可能出现最后倒的是一关键点的牌或某一段中的牌 
     5        
     6     最短路径: 
     7         1、先dij求出源点到每个点最短时间d[u],然后求得最大的一个;
     8         2、对于每段路程所花费的时间为(d[i]+d[j]+g[i][j])/2,然后比较求出最大的一个;
     9         如情况1得到的值比情况2的大,则输入一个点,否则输出该段的两个点;需要的时间则是两则间较大值 
    10 */
    11 #include<stdio.h>
    12 #include<string.h>
    13 #define inf 0x7fffff
    14 #define N 505
    15 int g[N][N];
    16 int time[N][N];
    17 int d[N],vis[N];
    18 int n,m,s,e,te;
    19 int dij(int u)
    20 {
    21     memset(vis,0,sizeof(vis));
    22     for(int i=0;i<=n;i++)
    23         d[i]=g[u][i];
    24     vis[u]=1;
    25     d[u]=0;
    26     for(int i=1;i<n;i++){
    27         int temp=inf;
    28         int v=u;
    29         for(int j=1;j<=n;j++)
    30             if(!vis[j] && d[j]<temp){
    31                 v=j;
    32                 temp=d[j];
    33             }      
    34         vis[v]=1;
    35         for(int j=1;j<=n;j++)
    36             if(!vis[j] && d[j]>temp+g[v][j])
    37                 d[j]=temp+g[v][j];
    38     }
    39 }
    40 double find_path()
    41 {
    42     double temp=-inf;
    43     for(int i=1;i<=n;i++)
    44         for(int j=i+1;j<=n;j++){
    45             if(g[i][j]<inf){
    46                 if((g[i][j]+d[i]+d[j])*1.0/2>temp){
    47                     temp=(g[i][j]+d[i]+d[j])*1.0/2;
    48                     s=i;
    49                     e=j;
    50                 }
    51             }
    52         }
    53     return temp;
    54 }
    55 int main(void)
    56 {
    57     int a,b,c,k=1;
    58     while(scanf("%d%d",&n,&m)!=EOF && (n+m))
    59     {
    60         for(int i=0;i<=n;i++)
    61             for(int j=0;j<=n;j++)
    62                 g[i][j]=inf;
    63         for(int i=0;i<m;i++){
    64             scanf("%d%d%d",&a,&b,&c);
    65             g[a][b]=g[b][a]=c;
    66         }
    67         printf("System #%d
    ",k++);
    68         dij(1);
    69         double dis=-inf; //初始化注意一下 
    70         for(int i=1;i<=n;i++)
    71             if(1.0*d[i]>dis){
    72                 dis=1.0*d[i];
    73                 te=i;
    74             }
    75         double path=find_path();
    76         if(path<=dis) printf("The last domino falls after %.1lf seconds, at key domino %d.
    ",dis,te);
    77         else printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.
    ",path,s,e);
    78         printf("
    ");
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    欣喜:终于找到了办法,可以把图片资源集成到dll文件之中了
    复习:关于类的继承和构造函数
    让Fckeditor支持中文——解决“Error loading "/fckeditor/fckstyles.xml" ”
    asp.net小技巧:保留password模式文本框textbox内的数据不丢失。
    数据库备份和恢复速记!
    资源:全球化与本地化(二)——指定语言语种
    一个Javascript类URI,顺便当作学习js类的笔记了。
    学习笔记:TimeSpan(时间片),以及各种时间差的算法
    ASP.NET MVC数据验证
    反射和属性
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3728453.html
Copyright © 2011-2022 走看看