poj_1135 Domino Effect

来源:互联网 发布:异常数据的剔除 编辑:程序博客网 时间:2024/05/04 02:51

Domino Effect

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 5637

Accepted: 1425

题目链接:http://poj.org/problem?id=1135

Description

Did you know that you can use domino bones for otherthings besides playing Dominoes? Take a number of dominoes and build a row bystanding 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 tothe opposite extreme in the early Eighties. Using millions of dominoes ofdifferent colors and materials to fill whole halls with elaborate patterns offalling dominoes, they created (short-lived) pieces of art. In these constructions,usually not only one but several rows of dominoes were falling at the sametime. 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 formedby dominoes, computes when and where the last domino falls. The system consistsof several ``key dominoes'' connected by rows of simple dominoes. When a keydomino falls, all rows connected to the domino will also start falling (exceptfor the ones that have already fallen). When the falling rows reach other keydominoes that have not fallen yet, these other key dominoes will fall as welland set off the rows connected to them. Domino rows may start collapsing ateither end. It is even possible that a row is collapsing on both ends, in whichcase 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 dominosystems. The first line of each description contains two integers: the number nof key dominoes (1 <= n < 500) and the number m of rows between them. Thekey dominoes are numbered from 1 to n. There is at most one row between anypair of key dominoes and the domino graph is connected, i.e. there is at leastone way to get from a domino to any other domino by following a series ofdomino rows.

The following m lines each contain three integers a, b, and l, stating thatthere is a row between key dominoes a and b that takes l seconds to fall downfrom 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 beprocessed.

Output

For each case output a line stating the number of thecase ('System #1', 'System #2', etc.). Then output a line containing the timewhen the last domino falls, exact to one digit to the right of the decimalpoint, and the location of the last domino falling, which is either at a keydomino or between two key dominoes(in this case, output the two numbers in ascendingorder). Adhere to the format shown in the output sample. The test data willensure 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

SouthwesternEuropean Regional Contest 1996

题意:

       多米诺骨牌的游戏,从第一个开始倒,计算最后一个倒了的时间比如第二个测试样例,从1倒向23共用5秒,从2倒向3和从2倒向2个用一半的时间,所以答案是7.5

解题思路:

        这个题目可以抽象成求解最短路问题,用Dijkstra()1到每一个的最短距离,取出最大的值,然后判断最后是只倒向一个或者倒向2个,如果倒向2个,那么其度数一定大于1,并且等于i的最短距离与到j的最短距离之和加上这条边的权值在除以2取最大值

代码:

#include <iostream>#include<stdio.h>#include<cstring>#define maxn 512#define VALUE 0xffffffusing namespace std;int g[maxn][maxn];int visited[maxn];double dis[maxn];int n ,m;double x,y;void Dijkstra(){    int i;    x=-1;    for(i=1;i<=n;i++)    {        visited[i]=0;        dis[i]=VALUE;    }    dis[1]=0;    while(true)    {        int t=-1;        for(i=1;i<=n;i++)        {            if(visited[i]==0 && (t==-1 || dis[i]<dis[t]))                t=i;        }        if(t==-1)            break;        visited[t]=1;        if(x<dis[t])            x=dis[t];        for(i=1;i<=n;i++)        {            if(g[i][t]!=-1 && dis[i]>dis[t]+g[i][t])            {                dis[i]=dis[t]+g[i][t];            }        }    }}int main(){    int count=0;    int i;    while(true)    {        count++;        int s,e,w;        scanf("%d%d",&n,&m);        if(!n && !m)            break;        memset(g,-1,sizeof(g));        for(i=0;i<m;i++)        {            scanf("%d%d%d",&s,&e,&w);            if(g[s][e]==-1 || g[s][e]>w)//如果有重边,取最小的那条            g[s][e]=w;            g[e][s]=w;        }        int a,b;Dijkstra();y=-1;        for(i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(g[i][j]!=-1)                {                    //到i的最短距离与到j的最短距离之和加上这条边的权值在除以2                    double temp=(dis[i]+dis[j]+g[i][j])/2;                    if(y<temp)                    {                        y=temp;                        a=i;                        b=j;                    }                }            }        }        if(n==1)//考虑到如果只有一张的话我0秒,并且key为1            b=1;        if(x>=y)        {            printf("System #%d\nThe last domino falls after %.1f seconds, at key domino %d.\n\n",count,x,b);        }        else        {            printf("System #%d\nThe last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",count,y,a,b);        }    }    return 0;}


 

原创粉丝点击