POJ1135_Domino Effect(最短路)

Domino Effect

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8224   Accepted: 2068

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

Southwestern European Regional Contest 1996

解题报告

最后倒下的牌有两情况,1是关键牌,而是两关键之间。

这题的特殊数据是1 0

#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 99999999
using namespace std;
int n,m,mmap[510][510],dis[510],vis[510];
void dij()
{
    int i,j,u,minn;
    for(i=1; i<=n; i++)
    {
        dis[i]=mmap[1][i];
        vis[i]=0;
    }
    dis[1]=0;
    vis[1]=1;
    for(i=0; i<n-1; i++)
    {
        minn=inf;
        u=0;
        for(j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                minn=dis[j];
                u=j;
            }
        }
        vis[u]=1;
        for(j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]>dis[u]+mmap[u][j])
            {
                dis[j]=dis[u]+mmap[u][j];
            }
        }
    }
}
int main()
{
    int i,j,u,v,w,k=1;
    while(cin>>n>>m)
    {
        if(!n&&!m)break;

        printf("System #%d\n",k++);
        if(n==1)
            {printf("The last domino falls after 0.0 seconds, at key domino 1.\n\n");
            continue;}
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
                mmap[i][j]=inf;
            mmap[i][j]=0;
        }
        for(i=0; i<m; i++)
        {
            cin>>u>>v>>w;
            if(mmap[u][v]>w)
                mmap[u][v]=mmap[v][u]=w;
        }
        dij();
        double maxx1=0;
        int u=0;
        for(i=1; i<=n; i++)
        {
            if(dis[i]>maxx1)
            {
                maxx1=dis[i];
                u=i;
            }
        }
        int a1,a2;
        double maxx2=0;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                double t=(dis[i]+dis[j]+mmap[i][j])/2.0;
                if(mmap[i][j]<inf&&maxx2<t)
                {
                    maxx2=t;
                    a1=i;
                    a2=j;
                }
            }
        }
        if(maxx1>=maxx2)
        {
            printf("The last domino falls after %.1lf seconds, at key domino %d.\n",maxx1,u);
        }
        else printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",maxx2,a1,a2);
        printf("\n");
    }
    return 0;
}

POJ1135_Domino Effect(最短路)

时间: 2024-11-03 09:22:04

POJ1135_Domino Effect(最短路)的相关文章

POJ 1135 Domino Effect(最短路 多米诺骨牌)

题意 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两张骨牌之间只有很短的空隙.如果排列得很好,当你推 倒第 1张骨牌,会使其他骨牌连续地倒下(这就是短语"多米诺效应"的由来). 然而当骨牌数量很少时,这种玩法就没多大意思了,所以一些人在 80 年代早期开创了另一个 极端的多米诺骨牌游戏:用上百万张不同颜色.不同材料的骨牌拼成一幅复杂的图案.他们开创 了一种流行的艺术.在这种骨牌游戏中,通常有多行骨牌同时

poj1135Domino Effect——最短路

题目:http://poj.org/problem?id=1135 先在图中跑一遍最短路,最后倒的牌可能是dis值最大的点,也可能是在dis值最大的点所连的边上,尝试一下即可: 坑:n=1的时候输出点1. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; queue<int>q; int n,m,head

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

图论500题

=============================以下是最小生成树+并查集====================================== [HDU] 1213   How Many Tables   基础并查集★ 1272   小希的迷宫   基础并查集★ 1325&&poj1308  Is It A Tree?   基础并查集★ 1856   More is better   基础并查集★ 1102   Constructing Roads  基础最小生成树★ 12