POJ 1135 Domino Effect 最短路

来源:互联网 发布:多货币对冲ea源码 编辑:程序博客网 时间:2024/05/16 02:12

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 11 2 273 31 2 51 3 52 3 50 0

Sample Output

System #1The last domino falls after 27.0 seconds, at key domino 2.System #2The last domino falls after 7.5 seconds, between key dominoes 2 and 3.题意:给出N张KEY多米诺骨牌(节点) 和M个关系  每个关系表示第一个KEY牌倒下到第二个KEY牌倒下要 l秒求最后倒下牌的位置和时间思路:最后倒下的牌又两种可能 如样例输出所示, 可能节点牌倒下 也可能两节点中间牌倒下a) 先计算每一张关键牌倒下的dis[i]。这需要利用Dijkstra 算法求第1 张关键牌到其他每张关键牌的最短路径。然后取dis[i]的最大值,设为maxtime1。b) 计算每一行完全倒下的时间。设每一行的两端的关键牌为i 和j,则这一行完全倒下的时间为(dis[i] + dis[j] + map[i][j])/2.0,其中map[i][j]为连接第i、j 两张关键牌的行倒下所花的时间。取所有行完全倒下时间的最大值,设为maxtime2。c) 如果maxtime2 > maxtime1,则是第②种情形;否则是第①种情形。代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define M  505using namespace std;int dis[M];int map[M][M];int vis[M];int mmax=9999999;int m;void dij(){    int i,j,pos;    int mmin;    for(i=1;i<=m;i++)    {        dis[i]=map[1][i];        vis[i]=0;    }    dis[1]=0; //注意第一张牌倒下的最小时间是0    vis[1]=1;    for(i=1;i<m;i++)    {         mmin=mmax;         for(j=1;j<=m;j++)         {             if(dis[j]<mmin&&!vis[j])             {                 pos=j;                 mmin=dis[j];             }         }         vis[pos]=1;         for(j=1;j<=m;j++)         {             if(dis[pos]+map[pos][j]<dis[j]&&!vis[j])                dis[j]=dis[pos]+map[pos][j];         }    }    return;}int main(){    int n,i,j,s;    int a,b;    int c;    int t=0;    while(~scanf("%d%d",&m,&n)){            t++;        if(m==0&&n==0)            break;            for(i=1;i<m;i++)                for(j=i;j<=m;j++)                    map[i][j]=map[j][i]=mmax;        while(n--){            scanf("%d%d%d",&a,&b,&c);            map[a][b]=map[b][a]=c;        }        dij();        double maxtime1=-1.0,maxtime2=-1.0;        double t2;        int pos1,pos2,pos3;        for(i=1;i<=m;i++)        {            if(dis[i]!=mmax&&dis[i]>maxtime1)            {                maxtime1=dis[i];                pos1=i;            }        }        for(i=1;i<=m;i++)            for(j=1;j<=m;j++)                     {            if(dis[i]!=mmax&&dis[j]!=mmax&&map[i][j]!=mmax&&i!=j)               {                 t2=(dis[i]+dis[j]+map[i][j])/2.0;                 if(t2>maxtime2)                   {                       maxtime2=t2;                       pos2=i;                       pos3=j;                   }            }        }    printf("System #%d\n",t);       if(maxtime1>=maxtime2)        printf("The last domino falls after %.1lf seconds, at key domino %d.\n\n",maxtime1,pos1);       else        printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n\n",maxtime2,pos2,pos3);    }    return 0;}


0 0
原创粉丝点击