poj 1135 Domino Effect (dijkratra算法)

Domino Effect

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8848   Accepted: 2210

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

题意:有很多骨牌,有一些关键牌,它们一旦倒下,和它们相连的各个普通行都倒下,求最后一个倒下的牌所用时间和位置。

注意:有的行特别长,所以有可能行中间的最后倒下。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
#define N 505
#define inf 0x7fffffff
int mark[N],g[N][N];
int dis[N];
void dijkstra(int s,int n)
{
    int i,u,min;
    for(i=1;i<=n;i++)
    {
        dis[i]=g[s][i];
        mark[i]=0;
    }
    mark[s]=1;
    while(1)
    {
        u=0;
        min=inf;
        for(i=1;i<=n;i++)
        {
            if(!mark[i]&&min>dis[i])
            {
                min=dis[i];
                u=i;
            }
        }
        if(u==0)
            break;
        mark[u]=1;
         for(i=1;i<=n;i++)
         {
             if(!mark[i]&&g[u][i]<inf&&dis[i]>dis[u]+g[u][i])
                dis[i]=dis[u]+g[u][i];
         }
    }
}
void work(int n)
{
    int i,j,k0,k1,k2;
    double t1,t2;
    t1=t2=-inf;          //有坑,时间可以为零
    for(i=1;i<=n;i++)
    {
        if(t1<dis[i])
        {
            t1=dis[i];k0=i;
        }
    }
    for(i=1;i<=n;i++)    //某一普通行可能很长很长
    {
        for(j=1;j<=n;j++)
        {
            if(g[i][j]<inf&&t2<(dis[i]+dis[j]+g[i][j])/2.0)
            {
                if(i<j) {k1=i;k2=j;}
                else {k1=j;k2=i;}
                t2=(dis[i]+dis[j]+g[i][j])/2.0;
            }
        }
    }
    if(t1<t2)
        printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",t2,k1,k2);
    else
        printf("The last domino falls after %.1f seconds, at key domino %d.\n",t1,k0);
}
int main()
{
    int i,j,n,m,u,v,w,cnt=1;
    while(scanf("%d%d",&n,&m),n||m)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                g[i][j]=(i==j?0:inf);
            }
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            g[u][v]=g[v][u]=w;
        }
        printf("System #%d\n",cnt++);
        dijkstra(1,n);
        work(n);
        puts("");
    }
    return 0;
}

poj 1135 Domino Effect (dijkratra算法)

时间: 2024-12-13 23:21:44

poj 1135 Domino Effect (dijkratra算法)的相关文章

POJ 1135.Domino Effect

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 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

POJ 1135 Domino Effect (Dijkstra 最短路)

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 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 t

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

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

POJ 1135 Domino Effect(最短路径)

题意有m排放置好的多米诺骨牌,每排的头尾骨牌称为“关键骨牌”,共有n个关键骨牌,每排多米诺倒下需要L秒,关键骨牌倒下的时间忽略不计.推倒关键骨牌1,求最后一个骨牌倒下的时间及位置.(若最后一个倒下的骨牌不是关键骨牌,则按升序输出这个骨牌所在的那一排的两端的关键骨牌) 样例输入2 11 2 273 31 2 51 3 52 3 50 0 样例输出System #1The last domino falls after 27.0 seconds, at key domino 2. System #2

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

POJ 3264 RMQ Spare Table算法

今天下午大帝讲的,我以前也不懂,所以也就跟着学学了,把中间的那个状态转移方程学错了好几次,于是就wa了 好几发. #include<iostream> #include<cstdio> #include<algorithm> #define maxn 200010 using namespace std; int a[maxn],m,n,b[maxn],fl[maxn][50],fr[maxn][50]; void solve() { b[1]=0;//其实就是用来计算

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

1135: 零起点学算法42——多组测试数据(求和)IV

1135: 零起点学算法42--多组测试数据(求和)IV Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2439  Accepted: 1277[Submit][Status][Web Board] Description 还有一些输入是以上几种情况的组合,具体根据题目对前面几种情况进行组合 比如题目要求是多组测试数据 每组测试数据首先输入一个整数n(如果n=0就表示结束) 然后再输入n个整数 这