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. 北京Uber优步司机奖励政策(3月17日)

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

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

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

  3. 北京Uber优步司机奖励政策(12月31日)

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

  4. 宁波Uber优步司机奖励政策(12月21日到12月27日)

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

  5. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  6. JAVA面试中问及HIBERNATE与 MYBATIS的对比

    第一方面:开发速度的对比 就开发速度而言,Hibernate的真正掌握要比Mybatis来得难些.Mybatis框架相对简单很容易上手,但也相对简陋些.个人觉得要用好Mybatis还是首先要先理解好H ...

  7. NavRouter

    使用方法只需要跟vue-router一样正常使用即可,这里我们新加了一个路由跳转方法nav: router.nav()//参数同router.replace一样. 路由跳转策略 首先说下路由跳转过程, ...

  8. InnoDB锁冲突案例演示

      Preface       As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...

  9. 「专题训练」k-Tree(CodeForces Round #247 Div.2 C)

    题意与分析(Codeforces-431C) 题意是这样的:给出K-Tree--一个无限增长的树,它的每个结点都恰有\(K\)个孩子,每个节点到它\(K\)个孩子的\(K\)条边的权重各为\(1,2, ...

  10. Linux命令应用大词典-第24章 任务计划

    24.1 contab:针对个人用户维护crontab文件