POJ—1135 Domino Effect

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jnxxhzz/article/details/83044827

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

扫描二维码关注公众号,回复: 4202025 查看本文章


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

猜你喜欢

转载自blog.csdn.net/jnxxhzz/article/details/83044827