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. aix-syslog

    收集网络内路由器的日志信息,同时把本地日志信息与路由器信息分开. /etc/syslog.conf我写成: ## 本地日志处理 *.notice;*.err;*.warn<tab>;< ...

  2. Python 中一个逗号引发的悲剧

    遇到一个 Python 字符串的坑,记录一下.看看下面这些代码 >>> a = [ ... 'foo' ... 'bar', ... 'tree' ... ] >>> ...

  3. 【独家】完美解决appium安装app时,需要手动确认安装的问题

    appium初始化driver时,如果未安装该app会先进行安装,安装时,很多安卓手机都会弹框,需要手动确认安装. 如小米的机器, 这是个头疼的问题,之前在网上找遍了,只有通过adb去点相对坐标成功了 ...

  4. 5.log4j报错

    java.lang.UnsupportedClassVersionError: org/apache/log4j/Logger : Unsupported major.minor version 51 ...

  5. 用jconsole监视内存使用情况

    最近做性能压测,学习到可以用jconsole查看内存使用(连接端口:JMX_PORT=8060). 打开后发现,老年代内存一直无法释放,应该是应用启动参数中,老年代内存分配不够.加大内存,得到缓解:- ...

  6. as3 string split方法一些注意

    split () 方法 AS3 function split(delimiter:*, limit:Number = 0x7fffffff):Array 如果指定 limit 参数,返回的数组中具有的 ...

  7. ubuntu16.04设置电池充电阈值

    thinkpad在安装ubuntu16.04之后,设置充电阈值: 方法一: 使用双系统,在windows下使用联想的Lenovo setting center设置之后,在ubuntu之下也可以保持相同 ...

  8. 显示AVI的第一桢

    procedure TForm1.Button1Click(Sender: TObject);begin  Application.ProcessMessages;  MediaPlayer1.Ope ...

  9. 机房servlet类实验

    源代码1: import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class accept extend ...

  10. pyplot图像组件

    pyplot图像组件 ax子对象的组件内容 Title 图表标题 plt.title() Axis 坐标范围,x轴,y轴 plt.axis() label 坐标轴标注 plt.xlabel() plt ...