1. 程式人生 > >【ZOJ1298】Domino Effect(dijkstra+思維)

【ZOJ1298】Domino Effect(dijkstra+思維)

題目連結


Domino Effect


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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.


Source: Southwest Europe 1996

 

【題意】

給定n張多米諾骨牌,給出m條多米諾骨牌的轉折的起始和終止位置,已經倒下的時間,計算最後一張骨牌倒下的位置,已經需要的時間。

【解題思路】

轉自大佬部落格:https://blog.csdn.net/jnxxhzz/article/details/83044827

(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<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
int edge[505][505],dis[505],vis[505];
void dijkstra()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dis[i]=INF;
    dis[1]=0;
    while(1)
    {
        int v=-1;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i] && (v==-1 || dis[v]>dis[i]))
                v=i;
        }
        if(v==-1)break;
        vis[v]=1;
        for(int i=1;i<=n;i++)
            dis[i]=min(dis[i],dis[v]+edge[v][i]);
    }
}
int main()
{
    int kase=1;
    while(~scanf("%d%d",&n,&m) && n || m)
    {
        int u,v,s;
        memset(edge,INF,sizeof(edge));
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&s);
            edge[u][v]=s;
            edge[v][u]=s;
        }
        dijkstra();
        printf("System #%d\n",kase++);
        double ans=-INF,ans2=-INF;
        int dot,dot1,dot2;
        for(int i=1;i<=n;i++)
        {
            if(ans<dis[i])
            {
                ans=dis[i];
                dot=i;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
            	if(edge[i][j]!=INF)
            	{
	                double time=(dis[i]*1.0+dis[j]*1.0+edge[i][j]*1.0)/2.0;
	                if(time>ans2)
	                {
	                    ans2=time;
	                    dot1=i;
	                    dot2=j;
	                }            		
				}
            }
        }
        if(ans>=ans2)printf("The last domino falls after %.1f seconds, at key domino %d.\n",ans,dot);
        else printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",ans2,dot1,dot2);
        printf("\n");
    }
    return 0;
}