1. 程式人生 > >POJ—1135 Domino Effect

POJ—1135 Domino Effect

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 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.

 

【分析】

題意:有一堆多米諾骨牌,轉折點的編號分別為1~n,然後給出m條邊,x,y,z表示x如果倒了y會在z秒後倒,y倒了x也會在z秒後倒,問現在推到編號為1的多米諾骨牌,最後一塊倒的牌是哪塊,如果正好是轉折點1~n那就是1~n,如果在某兩塊牌中間,那麼就輸出型如sample第二種的答案

首先考慮一件事情,我們要知道每塊牌倒的時間,那麼一定是從1開始到達這塊牌的最短時間,這個很好理解

那麼顯然我們需要處理一遍從1出發到達所有點的最短路....那這個就隨便處理了

處理完之後,考慮兩種情況

1.最後倒的牌是轉折點,那麼就是所有點的最短路中求個最大值

2.最後倒的牌是兩個轉折點中間的牌,那麼就是(起點到達這條邊兩個端點的最短時間+這條邊的長度)/2,求最大值

當然在第二種情況下這種寫法也計算了不應該計算的一些邊,因為如果這條邊上最後一塊倒下的牌如果在邊的中間,那麼這條邊的兩個端點到達起點的距離之差應該 小於 這條邊的長度才可以,那麼為什麼可以對每條邊都計算上面的答案呢?

因為顯然如果 倒下的牌不是在邊的中間,那麼也就是說明這條邊兩個端點到達起點的距離之差是這條邊的長度,至於為什麼是這條邊的長度...那就需要理解一下最短路了

也就是說如果轉化為公式就是(dis[i]+dis[j]+edge[i][j]) / 2

設dis[i]>dis[j],那麼dis[i]=dis[j]+edge[i][j]  , 那麼公式就變成了(dis[j] * 2 + edge[i][j] * 2 ) / 2 = dis[j] + edge[i][j] = dis[i]

所以不會對答案造成影響...所以為了程式碼簡便直接寫了..不需要增加判斷

 

然後判斷兩個最大值哪個大就可以知道最後一塊牌是哪塊了....

把題目轉化成最短路模型就好做了

【程式碼】

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int INF = 0x3f3f3f3f;
int dis[550];
int edge[550][550];
int vis[550];
int n,m;

void dijkstra(){
	memset(vis,0,sizeof(vis));
    dis[1]=0;
	vis[1]=1;
    for(int i = 2 ; i <= n ; ++i) dis[i] = edge[1][i];
    for(int i = 0 ; i < n - 1 ; ++i){
        int minn = INF , go = 0;
        for(int j = 1 ; j <= n ; ++j)
            if(!vis[j] && dis[j] < minn){
                go = j;
                minn = dis[j];
            }
        vis[go] = 1;
        for(int j = 1 ; j <= n ; ++j)
            if(!vis[j] && edge[go][j] < INF && 
				dis[j] > dis[go] + edge[go][j])
                dis[j] = dis[go] + edge[go][j];
    }
    double ans1 = -INF;
	int pos;
    for(int i = 1 ; i <= n ; ++i)
        if(dis[i] > ans1){
            ans1 = dis[i];
            pos = i;
        }
    double ans2 = -INF;
    int poss1,poss2;
	for(int i = 1 ; i <= n ; ++i){
        for(int j = i + 1 ; j <= n ; ++j)
			if (edge[i][j] < INF){
	            double now = (dis[i] + dis[j] + edge[i][j]) / 2.0;
	            if(now > ans2){
	            	ans2 = now;
	                poss1 = i;
	                poss2 = j;
	            }
        }
    }
    if(ans2 > ans1)
        printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",ans2,poss1,poss2);
    else
        printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",ans1,pos);
}

int main(){
    int pp = 0;
	while(scanf("%d%d",&n,&m) != EOF){
		if (n == 0 && m == 0) break;
    	printf("System #%d\n",++pp);
        memset(edge,INF,sizeof(edge));
		for(int i = 0 ; i < m ; ++i){
        	int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            edge[u][v]=w;
            edge[v][u]=w;
        }
        dijkstra();
    }
    return 0;
}