poj 1135最短路

Domino Effect
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6904   Accepted: 1754

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.

这是一道比较典型的求最短路的问题,依靠dijkstra为基础,即可解决。
题意过长,从别人的博客里直接截出来的。
多米诺骨牌有一种玩法,就是将它们立起来,然后拨倒第一个骨牌,它们将一个连带一个的倒下。现在 有很多的骨牌,分为两种,一种为一般的骨牌,在其前后分别只有一个骨牌;还有一种所谓的“关键骨牌",即其前后至少有一个骨牌,当它倒下时,与它相连的所 有未倒下的牌一起倒。可能有时骨牌两端一起向中间倒,这样所有的骨牌就好像一个图。现在知道从一个关键骨牌倒到另一个直接相连的关键骨牌(它们当中是一般 的骨牌)所需的时间。每个关键骨牌都有编号,当拨倒编号为1的骨牌,计算出整个骨牌全部倒下的时间和地点。每一个时间点都有可能有几路骨牌同时倒下,当然 要考虑最短的路线。
下面是代码:

#include<cstdio>
#include<cstring>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn=500;
const int inf=200000000;

int n,m;
int edge[maxn][maxn];
int caseno=1;
int time[maxn];
bool  vis[maxn];

void solve(){
     int i,j,k;
     for(i=0;i<n;i++){
          time[i]=edge[0][i];
          vis[i]=false;
     }
     time[0]=0;
     vis[0]=true;
     for(i=0;i<n-1;i++)
     {
          int min=inf,u=0;
          for(j=0;j<n;j++){
              if(!vis[j]&&time[j]<min){
                    min=time[u=j];
              }
          }
          vis[u]=true;
          for(k=0;k<n;k++){
              if(!vis[k]&&edge[u][k]<inf&&time[u]+edge[u][k]<time[k])
                   time[k]=time[u]+edge[u][k];
          }
     }
     double maxtime1=-inf;
     int pos;
     for(i=0;i<n;i++){
          if(time[i]>maxtime1){
              maxtime1=time[i];
              pos=i;
          }
     }
     double maxtime2=-inf;
     int pos1,pos2;
     double t;
     for(i=0;i<n;i++){
          for(j=0;j<n;j++){
             t=(time[i]+time[j]+edge[i][j])/2.0;
             if(edge[i][j]<inf&&t>maxtime2){
                       maxtime2=t;
                       pos1=i;
                       pos2=j;
             }
         }
     }
     printf( "System #%d\n",caseno++);
     printf( "The last domino falls after ");
     if(maxtime1<maxtime2)
         printf( "%.1f seconds, between key dominoes %d and %d.\n\n",maxtime2,pos1+1,pos2+1 );
     else
         printf( "%.1f seconds, at key domino %d.\n\n",maxtime1,pos+1 );
}

int read(){
    int i,j;
    int v1,v2,t;
    scanf("%d %d",&n,&m);
    if(n==0&&m==0)
         return 0;
    for(i=0;i<n;i++)
       for(j=0;j<n;j++)
           edge[i][j]=inf;
    for(i=0;i<m;i++){
         scanf("%d %d %d",&v1,&v2,&t);
         v1--;
         v2--;
         edge[v1][v2]=edge[v2][v1]=t;
    }
    return 1;
}

int main(){
    while(read())
        solve();
        return 0;
}


没有更多推荐了,返回首页