POJ 1135 Domino Effect (Dijkstra)

来源:互联网 发布:各项异性采样优化 编辑:程序博客网 时间:2024/05/21 15:49
Domino Effect
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6198 Accepted: 1580

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.

Source

Southwestern European Regional Contest 1996
题意:给你N个关键骨牌,和M个关键骨牌之间的联系。从关键骨牌A翻到到骨牌B翻到所用时间,可以忽略中间的骨牌。求最后一块翻到的普通骨牌是什么时候,在什么地方。
思路:可以想到由于关键骨牌的翻到与中间骨牌的状态无关,于是就成了求最短路径问题。但是在最后一块骨牌翻到的地方有两种情况:1.刚好在一个关键骨牌上;2.在两个关键骨牌之间的某处。第一种情况可以求从1关键骨牌到其他的最短路径中花费最大的那快,第二种情况则是落在其中可能的某两块之间,所以枚举所有可能的两个情况。两种情况所花时间较长的就是落在那种情况下。
最后还要注意当n==1时的特判,我在这里很惨的WA了还几次T.T
#include <stdio.h>#define maxn 501#define oo 10000000#define min(a,b) a<b?a:b#define max(a,b) a>b?a:bint map[maxn][maxn];int dis[maxn];bool vis[maxn];int n,m;void dijkstra(){    for(int i=2; i<=n; i++)        dis[i]=map[i][1],vis[i]=false;    dis[1]=0,vis[1]=true;    for(int i=0; i<n-1; i++)    {        int k=1;        int mn=oo;        for(int j=2; j<=n; j++)        {            if(!vis[j]&&mn>dis[j])            {                k=j;                mn=dis[j];            }        }        vis[k]=true;        for(int j=2; j<=n; j++)            if(!vis[j]&&map[j][k]<oo&&dis[j]>map[k][j]+dis[k])                dis[j]=map[k][j]+dis[k];    }}int main(){    int u,v,d,ca=1;    while(scanf("%d%d",&n,&m)!=-1)    {        if(n==0&&m==0) break;        for(int i=1; i<=n; i++)        {            map[i][i]=0;            for(int j=i+1; j<=n; j++)                map[i][j]=map[j][i]=oo;        }        while(m--)        {            scanf("%d%d%d",&u,&v,&d);            if(d<map[u][v])                map[u][v]=map[v][u]=d;        }        dijkstra();        int max1,max2;        double ans=0.0;        for(int i=1; i<=n; i++)            if(dis[i]>ans)            {                ans=(double)dis[i];                max1=i;            }        double t1,t2;        bool sign=false;        for(int i=1; i<=n; i++)//枚举所有可能情况            for(int j=1; j<=n; j++)            {                t1=min(dis[i],dis[j]);                t2=max(dis[i],dis[j]);                if(map[i][j]<oo)                {                    double t=t2+((double)map[i][j]+t1-t2)/2.0;                    if(t>ans)                    {                        ans=t;                        max1=min(i,j);                        max2=max(i,j);                        sign=true;                    }                }            }        printf("System #%d\n",ca++);        if(n==1)        {            printf("The last domino falls after 0.0 seconds, at key domino 1.\n\n");            continue;        }        if(!sign)            printf("The last domino falls after %.1f seconds, at key domino %d.\n",ans,max1);        else            printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",ans,max1,max2);        printf("\n");    }    return 0;}


原创粉丝点击