Dijkstra算法:
// ShellDawn
// POJ1135
// No.8
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
#define maxn 505
int n,m;
int E[maxn][maxn];
int visited[maxn];
int low[maxn];
void Dijkstra(){
MM(visited);
memset(low,0x3f,sizeof(low));
low[1] = 0;
for(int i=0;i<n;i++){
int loc = 0;
int key = 0x3fffffff;
for(int j=1;j<=n;j++){
if(visited[j] == 0 && low[j] < key){
loc = j;
key = low[j];
}
}
visited[loc] = 1;
for(int j=1;j<=n;j++){
if(E[loc][j] != 0){
low[j] = min(low[j],low[loc] + E[loc][j]);
}
}
}
}
int main(){
int T = 1;
while(~scanf("%d%d",&n,&m)&&n!=0){
if(T!=1) puts("");
printf("System #%d\n",T++);
MM(E);
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
E[a][b] = E[b][a] = c;
}
Dijkstra();
double ans = 0;
int loc = 1;
for(int i=1;i<=n;i++){
if(low[i] > ans){
ans = low[i];
loc = i;
}
}
bool flag = true;
int locA = 1,locB = 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(E[i][j] != 0){
double t = (low[i] + low[j] + E[i][j])*1.0/2;
if(t>ans){
locA = i;
locB = j;
ans = t;
flag = false;
}
}
}
}
if(flag){
printf("The last domino falls after %.1f seconds, at key domino %d.\n",ans,loc);
}else{
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",ans,min(locA,locB),max(locA,locB));
}
}
return 0;
}