poj 1135 Domino Effect 最短路

Domino Effect
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8711 Accepted: 2180

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

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(in this case, output the two numbers in ascending order). Adhere to the format shown in the output sample. The test data will ensure there is only one solution. 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

这一题题目读了好长时间终于读懂了,读懂后就发现是个比较简单的最短路问题。

题意:以多米诺骨牌为背景,给n个关键骨牌,m条边,接下来m行,a,b,t表示a到b该边骨牌全部倒下所花费的时间为t;当倒下的行到达其他还没有倒下的关键骨牌时,则这些关键骨牌也开始倒下,同时连接到该关键骨牌的所有行业开始倒下。求最后一块骨牌倒下的位子。

(1)先用Dijkstra算法算出1节点到所有节点的最短路,找出其中最长的为max1;

(2)计算每一行完全倒下的时间,每一行倒下的时间为(dist [ i ] + dist [ j ] + mp [ i ] [ j ] )/2.0;其中i,j为每一行的两个端点,计算得到max2;

(3)比较max1和max2的大小按情况输出。

ps:1  0也是合法数据,开始写成while(scanf("%d%d",&n,&m)&&n&&m)一直WA=。=一把辛酸泪啊。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#define MAXN 550
#define INF 1000000
typedef long long ll;
using namespace std;

int visit[MAXN];
int dist[MAXN],mp[MAXN][MAXN];
int n,m,cas;

void Dijkstra()
{
    int i,j,now;
    int  mi;
    memset(visit,0,sizeof(visit));
    memset(dist,INF,sizeof(dist));
    for (i=1;i<=n;i++)
        dist[i]=mp[1][i];
    dist[1]=0;
    visit[1]=1;
    for (i=1;i<=n;i++)
    {
        mi=INF;
        now=-1;
        for (j=1;j<=n;j++)
        {
            if (!visit[j]&&dist[j]<mi)
            {
                now=j;
                mi=dist[j];
            }
        }
        if (now==-1)
            break;
        visit[now]=1;
        for (j=1;j<=n;j++)
            if (!visit[j]&&mp[now][j]<INF&&dist[j]>mp[now][j]+dist[now])
                dist[j]=mp[now][j]+dist[now];
    }
    int zz;
    double max1=-INF,max2=-INF,xxx;
    for (i=1;i<=n;i++)    //找出max1
        if (dist[i]>max1)
        {
            max1=dist[i];
            zz=i;
        }
    int xx,yy;
    for (i=1;i<=n;i++)   //找出max2
        for (j=1;j<=n;j++)
            if (i!=j)
            {
                xxx=(dist[i]+dist[j]+mp[i][j])/2.0;
                if (mp[i][j]<INF&&max2<xxx)
                {
                    max2=xxx;
                    xx=i;yy=j;
                }
            }
    printf("System #%d\n",cas++);
    if (max1<max2)
        printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",max2,xx,yy);
    else
        printf("The last domino falls after %.1f seconds, at key domino %d.\n",max1,zz);
    printf("\n");
}

int main()
{
    int i,j;
    cas=1;
    while(scanf("%d%d",&n,&m))
    {
        if (n==0&&m==0)
        return 0;
        for (i=1;i<=n;i++)
            for (j=1;j<=n;j++)
                mp[i][j]=mp[j][i]=INF;
        int  t;
        int from,to;
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&from,&to,&t);
            mp[from][to]=mp[to][from]=t;
        }
        Dijkstra();
    }
    return 0;
}


转载于:https://www.cnblogs.com/i8888/p/4044026.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值