POJ - 1135 / ZOJ - 1298 (最短路,diijkstra)

1 篇文章 0 订阅

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 file 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 file ends with an empty system (with n = m = 0), which should not be processed.

Output

The input file 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 file ends with an empty system (with n = m = 0), which should not be processed.

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 个关键多米诺骨牌 ,m条路径,从 a 到 b 花费 l 时间,推到 1 号关键牌,求最后一个倒下骨牌的时间和位置。

思路

最后一个倒下的骨牌,要么是关键牌(这个关键牌到 1 号牌最短时间最长),时间是到这个关键牌的最短时间),要么是在两个关键牌之间,时间是这两个关键牌的最短时间之和+它么两个的时间除二。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;

int N,M,e[510][510],dis[510],v[510];
void dijstr(int sr)
{
    int i,j,k,u,mi;
    memset(v,0,sizeof(v));
    for(i=1; i<=N; i++)
        dis[i]=e[sr][i];
    v[sr]=1;
    for(i=1; i<N; i++)
    {
        u=0,mi=inf;
        for(j=1; j<=N; j++)
        {
            if(v[j]==0&&dis[j]<mi)
            {
                mi=dis[j];
                u=j;
            }
        }
        if(u==0) break;
        v[u]=1;
        for(k=1; k<=N; k++)
            if(e[u][k]<inf&&dis[k]>dis[u]+e[u][k])
                dis[k]=dis[u]+e[u][k];
    }
    return;
}

int main()
{
    int z=1;
    while(~scanf("%d%d",&N,&M)&&(N || M))
    {
        int i,j,k,t1,t2,t3;
        memset(e,inf,sizeof(e));
        for(i=1; i<=N; i++)
            e[i][i]=0;
        for(i=0; i<M; i++)
        {
            scanf("%d%d%d",&t1,&t2,&t3);
            e[t1][t2]=t3;
            e[t2][t1]=t3;
        }
        dijstr(1);
        int u=1;
        double mad=0;
        for(i=2; i<=N; i++)
        {
            if(dis[i]>mad)
            {
                mad=dis[i];
                u=i;
            }
        }
        int ps,pd;
        double mid=0;
        for(i=1; i<=N; i++)
        {
            for(j=1; j<=N; j++)
            {
                if(i==j) continue;
                if(e[i][j]<inf&&(dis[i]+dis[j]+e[i][j])/2.0>mid)
                {
                    mid=(dis[i]+dis[j]+e[i][j])/2.0;
                    ps=i,pd=j;
                }
            }
        }
        printf("System #%d\n",z++);
        if(mad>=mid)
            printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",mad,u);
        else
            printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",mid,ps,pd);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值