Poj 1135 Shortest Path

Source: Internet
Author: User
Domino Effect
Time Limit:1000 MS   Memory Limit:65536 K
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 Hals 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 (could t 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 shocould 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.
 
This is a typical problem of the shortest circuit. It can be solved based on dijkstra.
The question is too long to be taken out from others' blogs.
Domino has a way of playing, that is, setting them up and then dumping the first card, and they will fall one by one. There are now a lot of bone cards, divided into two types, one is a general bone card, there is only one bone card before and after it; there is also a so-called "key bone card ", that is to say, there is at least one card before and after it. When it falls down, all the cards connected to it fall together. Sometimes the two ends of the dominoes fall together in the middle, so that all the dominoes are like a picture. Now you know the time needed to move from a key card to another key card that is directly connected (which is a common card. Each key card has a number. When you dial the card with a number of 1, calculate the time and location of the entire card. Each time point may have several dominoes falling down at the same time. Of course, you must consider the shortest route.
The following code is used:
#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.