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. Installing python-ldap in Ubuntu

    These are the steps to be followed to install python-ldap in Ubuntu. At first, sudo apt-get install ...

  2. 深圳Uber司机本周(7.13-7.19凌晨4:00)的奖励政策

    本周(7.13-7.19凌晨4:00)的奖励政策为: 佣金返还: 车费的20%适用于所有产品(不包括Tesla)无获取条件 翻倍补贴: 每周一到周四07:00-10:00/17:00-22:00:车费 ...

  3. Java:xxx is not an enclosing class

    1. 错误原因 该错误一般出现在对内部类进行实例化时,例如 public class A{ public class B{ } } 此时B是A的内部类,如果我们要使用如下语句实例化一个B类的对象: A ...

  4. 封装一个Automapper单例

    public class DataModule : IModule { public void Configure(IMapperConfigurationExpression cfg) { //cf ...

  5. 关于 NPOI 导出的 Excel 出现“部分内容有问题” 的解决方法

    近期发现使用 NPOI 导出的 Excel 文件,有部分用户反映在打开时报错,测试了一下,发现在低版本的 Office 中(2003版,配合2007格式兼容包)打开正常,但在高版本 Office 中, ...

  6. AIX7.1删除大批量文件(百万级、千万级)

    假设/data/test目录下含有数百万上千万的文件需要删除,可以选择的方式如下: 1.如果文件名不包含空白符.引号等特殊字符,则可以使用如下命令: find /data/test -type f | ...

  7. ubuntu 14.04 lts LAMP配置

    一.目标 创建服务器环境,主要包括:Apache2.4.7 serverPHP 5.5.9Mysql 5.5.49扩展:MemcacheMcrypt 二.准备工作 1.服务器系统版本 Ubuntu s ...

  8. Soliworks 2016建模细节总结(1)

    Soliworks 2016建模小细节总结(1) 1.Solidworks 2016三维建模的时候,如果想要在一个视图里面呈现出四个视图(包括三个独立的视图以及三维结构的实体模型图),可以直接按鼠标会 ...

  9. HDU - 6444(单调队列+思维)

    链接:HDU - 6444 题意:给出一个包含 n 个数的环,每个数都有一个价值,起点任选,每次跳顺时针跳 k 个数,在哪个数就能获得该价值(包括起点),最多取 m 次,问最少需要补充多少价值,所拿的 ...

  10. kubernetes相关

    1.获取client , api-server 加token 或in-cluster方式 2.所有对象均有list update get 等方法 3.对象属性源码追踪,yaml与源码一一对应 4.一些 ...