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. 【POJ2182】Lost Cows

    [POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...

  2. PHP中URL字符串与关联数组的互相转换

    转换PHP数组为查询字符串放到URL中 $data = array( 'apikey'=>'xg6tr7k', 'user'=>'abcd', 'email'=>'root@exam ...

  3. php session存入redis

    php的会话默认以文件的形式存在,可以配知道NOSQL中,既可以提高访问速度又能好好的实现回话共享,在后期做负载均衡时实现多台服务器session 同步也是比较方便: 一:在php配置文件中改 修改p ...

  4. 「国庆训练」Bomb(HDU-5934)

    题意 给定\(n\)个炸弹,每个炸弹的坐标与代价与影响范围给定,炸弹会引爆影响范围内其他所有炸弹.求引爆所有炸弹的最小代价. 分析 先做\(n^2\)的循环,然后建图,对\(i\)能引爆\(j\)建边 ...

  5. 第四篇 与Flask相关的插件(flask-session、wtforms)

    公司中使用SQL的种方式: 1. 写Django:ORM(关系对象映射), 2. 写Flask和其他:有两种方式: (1) 原生SQL:使用原生SQL有两种选择: A. pymysql (python ...

  6. git服务器搭建及eclipse使用git

    一.搭建git服务器 1.yum install git 2.新建用户linux用户git,管理git服务 useradd git passwd git 3.初始化git仓库 git init --b ...

  7. [JSON].connectionValue()

    语法: [JSON].connectionValue() 说明: 将对象的所有键值接连成新的字符串值 返回: [String] 示例: Set a = toJson() c = Array(1,2,3 ...

  8. Python3 集合

    1.集合的表示 集合是一个无序不重复的元素序列 创建空集合 set() 2.集合的运算 a={1,2,3} b={2,3,4} print(a-b) #a中包含b中不包含 print(a|b) #a中 ...

  9. Spring Boot - Filter实现简单的Http Basic认证

    Copy自http://blog.csdn.net/sun_t89/article/details/51916834 @SpringBootApplicationpublic class Spring ...

  10. Ext JS 6学习文档-第5章-表格组件(grid)

    Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...