【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;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/83108079