• TOJ 1883 Domino Effect


    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.

    Source

    Southwestern Europe 1996

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <queue>
     4 #include <vector>
     5 #define MAXN 600
     6 #define inf 0x3f3f3f3f
     7 using namespace std;
     8 
     9 struct Node{
    10     int end;
    11     double dis;    
    12 };
    13 
    14 int n,m;
    15 double dist[MAXN];
    16 vector<Node> V[MAXN];
    17 
    18 void spfa(){
    19     for(int i=1; i<=n; i++,dist[i]=inf);
    20     dist[1]=0;
    21     queue<Node> Q;
    22     Node n1;
    23     n1.end=1;
    24     n1.dis=0;
    25     Q.push(n1);
    26     while( !Q.empty() ){
    27         Node now=Q.front();
    28         Q.pop();
    29         for(int i=0; i<V[now.end].size(); i++){
    30             Node temp=V[now.end][i];
    31             double v=temp.dis+now.dis;
    32             if( v < dist[temp.end]){
    33                 dist[temp.end]=v;
    34                 temp.dis=v;    
    35                 Q.push(temp);
    36             }
    37         }    
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     int c=0;
    44     while( scanf("%d %d",&n ,&m)!=EOF ){
    45         if(n==0 && m==0)break;
    46         for(int i=1; i<=n; i++){
    47             V[i].clear();
    48         }
    49         int a,b,l;
    50         for(int i=0; i<m; i++){
    51             scanf("%d %d %d",&a ,&b ,&l);
    52             Node n1,n2;
    53             n1.end=b;
    54             n1.dis=l;
    55             V[a].push_back(n1);
    56             n2.end=a;
    57             n2.dis=l;
    58             V[b].push_back(n2);
    59         }
    60         spfa();
    61         double ans=-1;
    62         int k=0;
    63         for(int i=1; i<=n; i++){
    64             if(dist[i]>ans){
    65                 ans=dist[i];
    66                 k=i;
    67             }
    68         }
    69         int flag=0,t1,t2;
    70         for(int i=2; i<=n; i++){
    71             for(int j=0; j<V[i].size(); j++){
    72                 int to=V[i][j].end;
    73                 double dis=V[i][j].dis;
    74                 if( (dist[i]+dis+dist[to])/2>ans ){
    75                     flag=1;
    76                     ans=(dist[i]+dis+dist[to])/2;
    77                     t1=i;
    78                     t2=to;
    79                 }
    80             }
    81         }
    82         printf("System #%d
    ",++c);
    83         if(flag){
    84             printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.
    "
    85                 ,ans ,min(t1,t2) ,max(t1,t2));
    86         }else{
    87             printf("The last domino falls after %.1lf seconds, at key domino %d.
    ",ans,k);
    88         }
    89         puts("");
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    javascript——表单校验工具代码
    java工具类——字符串类型的时间格式转换为Timestamp类型
    log4j:WARN Please initialize the log4j system properly
    CentOS 网络配置
    linux-vim 快捷键
    2020年7月29日 数组操作练习
    2020年7月28日数组的声明,静态、动态初始化,遍历,内存分析
    2020年7月27日 练习题4、5、6、7、8、9、12
    2020年7月24日 100以内猜数字,统计次数
    2020年7月24日 编写FooBizBaz.java
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3569107.html
Copyright © 2020-2023  润新知