Description
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 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
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.
題目大意:給你n個關鍵的多米諾骨牌,這n個關鍵的多米諾骨牌由m條由骨牌組成的“路”相連,每條路都有自己的“長度”,當這n個骨牌中的任意一個骨牌 k 倒塌時,與k相連的所有“路”上的骨牌也會隨之而倒,讓你求把骨牌 1 推到後,所有骨牌中最後一個倒塌的骨牌距離骨牌1的最短距離。
解題思路:題目中保證圖是連通的,我們可以先求出骨牌1到其他(n - 1)個關鍵骨牌的最短距離,得到這些距離中的最大值MAX,然後枚舉圖中的每條邊,再更新MAX,具體詳解請看程序:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std ;
int n , m ;
const int MAXN = 505 ;
struct Node
{
int adj ;
double dis ;
};
const int INF = 0x7fffffff ;
int t ;
vector<Node> vert[MAXN] ;
double d[MAXN] ; // 保存頂點 1 到其他(n - 1)個頂點的最短距離
void clr() // 初始化
{
int i ;
for(i = 0 ; i < MAXN ; i ++)
vert[i].clear() ;
memset(d , 0 ,sizeof(d)) ;
}
void init()
{
clr() ;
int i , j ;
Node tmp ;
for(i = 0 ; i < m ; i ++) // 用鄰接表建圖
{
int a , b ;
double c ;
scanf("%d%d%lf" , &a , &b , &c) ;
tmp.adj = b ;
tmp.dis = c ;
vert[a].push_back(tmp) ;
tmp.adj = a ;
tmp.dis = c ;
vert[b].push_back(tmp) ;
}
}
queue<int> q ;
bool inq[MAXN] ;
void spfa(int u) // 求最短路
{
while (!q.empty())
q.pop() ;
q.push(u) ;
inq[u] = true ;
d[u] = 0 ;
int tmp ;
Node v ;
while (!q.empty())
{
tmp = q.front() ;
q.pop() ;
inq[tmp] = false ;
int i ;
for(i = 0 ; i < vert[tmp].size() ; i ++)
{
v = vert[tmp][i] ;
if(d[tmp] != INF && d[tmp] + v.dis < d[v.adj])
{
d[v.adj] = d[tmp] + v.dis ;
if(!inq[v.adj])
{
q.push(v.adj) ;
inq[v.adj] = true ;
}
}
}
}
}
void solve()
{
memset(inq , 0 , sizeof(inq)) ;
int i , j ;
for(i = 1 ; i <= n ; i ++)
{
d[i] = INF ;
}
spfa(1) ;
double MAX = d[1] ;
int MAXb = 1 ;
for(i = 1 ; i <= n ; i ++)
{
if(MAX < d[i])
{
MAX = d[i] ;
MAXb = i ;
}
}
int pan = 0 ;
int t1 , t2 ;
for(i = 1 ; i <= n ; i ++) // 枚舉每條邊 , 更新MAX
{
for(j = 0 ; j < vert[i].size() ; j ++)
{
Node tn = vert[i][j] ;
int ta = tn.adj ;
double td = tn.dis ;
if((d[i] + d[ta] + td) / 2 > MAX ) // 注意:最大距離的求法
{
pan = 1 ;
MAX = (d[i] + d[ta] + td) / 2;
if(i < ta)
{
t1 = i ;
t2 = ta ;
}
else
{
t1 = ta ;
t2 = i ;
}
}
}
}
printf("The last domino falls after %.1f seconds," , MAX) ;
if(pan)
{
printf(" between key dominoes %d and %d.\n" , t1 , t2) ;
}
else
{
printf(" at key domino %d.\n" , MAXb) ;
}
puts("") ;
}
int ca ;
int main()
{
ca = 0 ;
while (scanf("%d%d" , &n , &m) != EOF)
{
if(n == 0 && m == 0)
break ;
init() ;
printf("System #%d\n" , ++ ca) ;
solve() ;
}
return 0 ;
}