zoj 1298 Domino Effect (Dijkstra)

本文介绍了一种模拟多米诺骨牌连续倒塌过程的方法。通过计算最短路径,确定了多米诺骨牌系统中最后一块或多块骨牌倒塌的时间和位置。采用Dijkstra算法求解关键节点间的最短距离,进而分析整个系统的倒塌顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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开始,所以可以先统计1到其他的最短路dist[i]。然后倒下的牌有两种情况:1.最后倒下的牌是关键牌,其时间及位置就是第1张关键牌到其他关键牌中最短路的最大值及对应的关键牌。2.最后倒下的牌是两张关键牌之间的某张普通牌,其时间为这两张关键牌倒下时间的一半再加上这一行倒下时间的一半。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 10000000

double Edge[502][502];
double dist[502];
int s[502];
int n,m;

bool init()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
          Edge[i][j]=MAXN;
    }
}

void dijkstra(int v0)
{
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        dist[i]=Edge[v0][i];
    }
    s[v0]=1;
    dist[v0]=0;
    for(i=0;i<n-1;i++)
    {
        int min=MAXN;
        int u=v0;
        for(j=1;j<=n;j++)
        {
            if(!s[j] && dist[j]<min)
            {
                u=j;
                min=dist[j];
            }
        }
        s[u]=1;
        for(k=1;k<=n;k++)
        {
            if(!s[k] && Edge[u][k]<MAXN && dist[u]+Edge[u][k]<dist[k])
            {
                dist[k]=dist[u]+Edge[u][k];
            }
        }
    }
}

int main()
{
   int i,j;
   int u,v,w;
   int c=0;
   while(cin>>n>>m && (n||m))
   {
       c++;
       init();
       for(i=1;i<=m;i++)
        {
            cin>>u>>v>>w;
            Edge[u][v]=w;
            Edge[v][u]=w;
        }
          memset(s,0,sizeof(s));
          dijkstra(1);
      /*for(i=1;i<=n;i++)
        cout<<dist[i]<<" ";
      cout<<endl;
      */
      double maxn1=0;
      int num1=1;
      for(i=1;i<=n;i++)
      {
         if(dist[i]>maxn1)
         {
             maxn1=dist[i];
             num1=i;
         }
      }
      double maxn2=0;
      int num2=1,num3=1;
       for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
       {
           if(Edge[i][j]<MAXN)
           {
               if((dist[i]+dist[j]+Edge[i][j])/2>maxn2)
               {
                   num2=i;num3=j;
                   maxn2=(dist[j]+dist[i]+Edge[i][j])/2.0;
               }
           }
       }
       printf("System #%d\n",c);
       if(maxn2>maxn1)
       {
           printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",maxn2,num2,num3);
           cout<<endl;
       }
       else
       {
           printf("The last domino falls after %.1lf seconds, at key domino %d.\n",maxn1,num1);
           cout<<endl;
       }
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值