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 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. 题目链接:http://poj.org/problem?id=1135

题目意思:输入n,m表示有n张关键牌,m表示n张牌之间有m行普通牌进行连接。n张牌的编号为1~n。每两张关键牌之间之多只有一行普通牌,并且图案是联通的。从第1张关键牌推到。最后倒下的如果是关键牌,输出看样例;如果是普通牌,输出看样例。

思路:因为起点固定,可以用求最短路径的Dijkastra算法(也可以用BFS搜索,如果时间更少就入列)。求出每张关键牌的倒下时间。两个关键牌i,j之间的普通牌完全倒下的时间为(edge[i][j]+time[i]+time[j])*1.0/2.0;如果时间不是等于time[i]或者time[j],时间就是这行普通牌最后倒下的时间;否则最后倒下的牌就是关键牌;


代码:

#include<iostream>
#include<cstdio>
using namespace std;
#define INF 10000000
int n,m;
int edge[][];
int sign[],time[];
void Init()
{
int i,j;
int u,v,w;
for(i=; i<=n; i++)
{
time[i]=INF;
sign[i]=;
for(j=; j<=n; j++)
edge[i][j]=INF;
}
for(i=; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]=edge[v][u]=w;
}
}
void Dijkstra(int v0)
{
int i,j,t;
time[v0]=;
for(i=; i<n; i++)
{
sign[v0]=;
int Min=INF;
for(j=; j<=n; j++)
{
if(edge[v0][j]<INF&&time[v0]+edge[v0][j]<time[j])
time[j]=time[v0]+edge[v0][j];
if(sign[j]==&&time[j]<Min)
{
Min=time[j];
t=j;
}
}
v0=t;
if(v0<=&&v0>n) break;
}
}
int main()
{
int i,j;
int t=;
while(scanf("%d%d",&n,&m)&&!(n==&&m==))
{
Init();
Dijkstra();
float Max1=;
int flag=;
for(i=; i<=n; i++)
if(time[i]>Max1)
{
Max1=time[i];
flag=i;
}
float Max2=;
int a=,b=;
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
if(edge[i][j]<INF)
{
float ans=(edge[i][j]+time[i]+time[j])*1.0/;
if(ans>Max2)
{
Max2=ans;
a=i;
b=j;
}
}
}
cout<<"System #"<<t<<endl;
if(Max2>Max1) printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",Max2,a,b);
else printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",Max1,flag);
t++;
}
return ;
}

POJ 1135.Domino Effect Dijkastra算法的更多相关文章

  1. POJ 1135 -- Domino Effect(单源最短路径)

     POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...

  2. POJ 1135 Domino Effect (Dijkstra 最短路)

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 Des ...

  3. POJ 1135 Domino Effect(Dijkstra)

    点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...

  4. POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  5. [POJ] 1135 Domino Effect

    Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...

  6. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  7. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

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

    1135: 零起点学算法42--多组测试数据(求和)IV Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted ...

  9. UVA211-The Domino Effect(dfs)

    Problem UVA211-The Domino Effect Accept:536  Submit:2504 Time Limit: 3000 mSec  Problem Description ...

随机推荐

  1. (c#) 销毁资源和释放内存

    0. 什么是资源? .NET 框架中如何访问资源? 所谓的资源就是程序中可利用的数据,譬如:字符串.图片和任何二进制数据,包括任何类型的文件. 在面向对象的环境中,每一个类型都标识为某些程序所用的资源 ...

  2. bat批处理(二):%0 %1——给批处理脚本传递参数

    初次接触批处理脚本觉得有点意思,所以决定写一个小功能试验一下,谁知刚一开始就发现遇到了麻烦,本想着使用参数来控制程序的运行结果,可是参数怎么传进去呢,于是研究了一番,最终发现这个参数的传递与main函 ...

  3. python post提交数据

     使用utf8编码,value是上传的值 1.上传经纬度等数据http://112.74.44.47/VehicleWeb/Acceleration?gps=gpsValue&accelera ...

  4. ssh连接失败,提示 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    [root@iZ2ze4kh1rvftq4cevdfjwZ ~]# ssh IP @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  5. Alpha版本检测报告

    1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...

  6. DDD-002

    项目实践:http://www.cnblogs.com/daoqidelv/p/7499662.html#_label0 https://www.cnblogs.com/lonelyxmas/p/79 ...

  7. hashlib 加密模块使用说明

    import hashlib  #hashilib 模块 m = hashlib.md5() m.update('hello 天王盖地虎'.encode(encoding = 'utf-8)) m.h ...

  8. C++ 0x 使用可变参数模板类 实现 C# 的委托机制

    #ifndef _ZTC_DELEGATE_H_ #define _ZTC_DELEGATE_H_ #include <vector> #include <functional> ...

  9. 写一个singleton

    第一种:饱汉模式 public class SingleTon { private SingleTon(){ } //实例化放在静态代码块里可提高程序的执行效率,但也可能更占用空间 private f ...

  10. Recursion递归

    /*java.lang 核心包 如 String Math Integer System Thread等 拿来直接用 * java.awt 窗口工具 GUI * java.net 网络包 * java ...