Domino Effect


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 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 input 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. Adhere to the format shown in the output sample. If you find several solutions, output only one of them. 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: Southwest Europe 1996

有点老也是不错的一道题。

 //Accepted    1298    C++    0    2168    姜伯约
/*
题意:
从1开始推牌,问多少时间后牌全倒,可能出现最后倒的是一关键点的牌或某一段中的牌 最短路径:
1、先dij求出源点到每个点最短时间d[u],然后求得最大的一个;
2、对于每段路程所花费的时间为(d[i]+d[j]+g[i][j])/2,然后比较求出最大的一个;
如情况1得到的值比情况2的大,则输入一个点,否则输出该段的两个点;需要的时间则是两则间较大值
*/
#include<stdio.h>
#include<string.h>
#define inf 0x7fffff
#define N 505
int g[N][N];
int time[N][N];
int d[N],vis[N];
int n,m,s,e,te;
int dij(int u)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
d[i]=g[u][i];
vis[u]=;
d[u]=;
for(int i=;i<n;i++){
int temp=inf;
int v=u;
for(int j=;j<=n;j++)
if(!vis[j] && d[j]<temp){
v=j;
temp=d[j];
}
vis[v]=;
for(int j=;j<=n;j++)
if(!vis[j] && d[j]>temp+g[v][j])
d[j]=temp+g[v][j];
}
}
double find_path()
{
double temp=-inf;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
if(g[i][j]<inf){
if((g[i][j]+d[i]+d[j])*1.0/>temp){
temp=(g[i][j]+d[i]+d[j])*1.0/;
s=i;
e=j;
}
}
}
return temp;
}
int main(void)
{
int a,b,c,k=;
while(scanf("%d%d",&n,&m)!=EOF && (n+m))
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
g[i][j]=inf;
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=c;
}
printf("System #%d\n",k++);
dij();
double dis=-inf; //初始化注意一下
for(int i=;i<=n;i++)
if(1.0*d[i]>dis){
dis=1.0*d[i];
te=i;
}
double path=find_path();
if(path<=dis) printf("The last domino falls after %.1lf seconds, at key domino %d.\n",dis,te);
else printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",path,s,e);
printf("\n");
}
return ;
}

zoj 1298 Domino Effect (最短路径)的更多相关文章

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

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

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

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

  3. POJ 1135 Domino Effect(Dijkstra)

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

  4. POJ 1135.Domino Effect Dijkastra算法

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 De ...

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

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

  6. 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 ...

  7. UVA211-The Domino Effect(dfs)

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

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

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

  9. TOJ 1883 Domino Effect

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

随机推荐

  1. BZOJ3293_分金币_KEY

    题目传送门 设x[i]表示i+1向i传的糖果数,x[n]表示1向n传的糖果数,a'=(a[1]+...a[N])/N a[1]+x[1]−x[n]=a' a[2]+x[2]−x[1]=a' a[3]+ ...

  2. 北京Uber优步司机奖励政策(1月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 苏州Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. day 11 绘制轮廓

    #-*- coding:utf-8 -*- import cv2 import numpy as np #1.导入图像 #img = cv2.imread("home.jpg",0 ...

  5. java String matches 正则表达

    package test; /** * 在String的matches()方法,split()方法中使用正则表达式. * @author fhd001 */ public class RegexTes ...

  6. mysql c 获取error_code

    #include <stdio.h> #include <mysql.h> int main(int argc, char **argv) { MYSQL *con = mys ...

  7. python操作字符串内容并重新输出

    今天在做一个函数的作业,题目如下: 编写一个函数实现大写转小写,小写变大写,并且转换为镜像字符串,并且将字符串变为镜像字符串. 例如:’A’变为’Z’,’b’变为’y 示范字符串: ”sdSdsfdA ...

  8. JAVA Map 之元素定位,冲突碰撞

    基本特性: 维持健值对的集合接口,健不可以重复,每一个健只能映射到一个值. Map替代了原来的虚拟类Directory. Map提供了三种集合视角,keys(KeySet),values(Values ...

  9. java核心技术 笔记

    一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...

  10. Halcon和visionPro的比较

    很多朋友会问到visionpro和halcon这两款机器视觉软件,到底学哪个好呢,今天重码网就给大家讲一讲: 首先比较下两者的优缺点: halcon: 提供的图像算法要比Visionpro多,也就是说 ...