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.

Source

Southwestern Europe 1996

 #include <stdio.h>
#include <iostream>
#include <queue>
#include <vector>
#define MAXN 600
#define inf 0x3f3f3f3f
using namespace std; struct Node{
int end;
double dis;
}; int n,m;
double dist[MAXN];
vector<Node> V[MAXN]; void spfa(){
for(int i=; i<=n; i++,dist[i]=inf);
dist[]=;
queue<Node> Q;
Node n1;
n1.end=;
n1.dis=;
Q.push(n1);
while( !Q.empty() ){
Node now=Q.front();
Q.pop();
for(int i=; i<V[now.end].size(); i++){
Node temp=V[now.end][i];
double v=temp.dis+now.dis;
if( v < dist[temp.end]){
dist[temp.end]=v;
temp.dis=v;
Q.push(temp);
}
}
}
} int main()
{
int c=;
while( scanf("%d %d",&n ,&m)!=EOF ){
if(n== && m==)break;
for(int i=; i<=n; i++){
V[i].clear();
}
int a,b,l;
for(int i=; i<m; i++){
scanf("%d %d %d",&a ,&b ,&l);
Node n1,n2;
n1.end=b;
n1.dis=l;
V[a].push_back(n1);
n2.end=a;
n2.dis=l;
V[b].push_back(n2);
}
spfa();
double ans=-;
int k=;
for(int i=; i<=n; i++){
if(dist[i]>ans){
ans=dist[i];
k=i;
}
}
int flag=,t1,t2;
for(int i=; i<=n; i++){
for(int j=; j<V[i].size(); j++){
int to=V[i][j].end;
double dis=V[i][j].dis;
if( (dist[i]+dis+dist[to])/>ans ){
flag=;
ans=(dist[i]+dis+dist[to])/;
t1=i;
t2=to;
}
}
}
printf("System #%d\n",++c);
if(flag){
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n"
,ans ,min(t1,t2) ,max(t1,t2));
}else{
printf("The last domino falls after %.1lf seconds, at key domino %d.\n",ans,k);
}
puts("");
}
return ;
}

TOJ 1883 Domino Effect的更多相关文章

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

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

  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 (spfa + 枚举)- from lanshui_Yang

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

  5. UVA211-The Domino Effect(dfs)

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

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

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

  7. POJ 1135.Domino Effect Dijkastra算法

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

  8. zoj 1298 Domino Effect (最短路径)

    Domino Effect Time Limit: 2 Seconds      Memory Limit: 65536 KB Did you know that you can use domino ...

  9. [POJ] 1135 Domino Effect

    Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...

随机推荐

  1. delphi计算两个时间差

    uses DateUtils; var S1, S2: string; T1, T2: TDateTime; D, H, M, S: Integer; Value: Int64; begin S1 : ...

  2. 如何较为方便的在GMap.Net中实现车辆运行轨迹

    一.简单的思路 要实现车辆运行轨迹,我们可能需要一个定时触发的机制用来更新Marker的位置,除了位置移动,我们可能还需要动态改变车辆的方向,如下图: 首先,位置移动是最简单的,关键是方向的动态改变如 ...

  3. SourceTree使用

    SourceTree的基本使用   1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 window.mac可用 2. 获取项目代码 1. 点击克隆/新建 2. ...

  4. 删除当前文件夹的bat工具

    @echo off:11set /p path=Please enter delete filepath:del /f /s /q %path%rd /q /s %path%goto 11pause

  5. HTTP总结

    参考: https://www.cnblogs.com/fuqiang88/p/5956363.html https://www.cnblogs.com/zlingh/p/5887143.html h ...

  6. sed 增删改查详解以及 sed -i原理

    我为什么要详细记录sed命令:     sed 擅长取行.工作中三剑客使用频率最高,本篇文章将对sed命令常用的 增,删,改,查 进行详细讲解,以备以后工作中遗忘了查询,sed命令是作为运维人员来说, ...

  7. xcode9 上传app后iTues 构建版本不显示

    1.问题原因 苹果公司更新了ios10系统和xcode9以后,做了许多调整,如果开发者没有注意就会遇到这样那样的问题.作者在更新以后就遇到了上传app到appstore成功后,没有显示的问题.下面就介 ...

  8. 如何安装memcached

    软件的下载,好像从官网上只能下载未经编译的源码,需要自己编译后才能安装使用,不熟悉的用户还是直接百度搜索下载比较好,这里也提供一个下载地址给大家参考. www.newasp.net/soft/6373 ...

  9. CString、string、string.h的区别

    CString.string.string.h的区别   CString:CString是MFC或者ATL中的实现,是MFC里面封装的一个关于字符串处理的功能很强大的类,只有支持MFC的工程才能使用. ...

  10. 洛谷P3358 最长k可重区间集问题(费用流)

    传送门 因为一个zz错误调了一个早上……汇点写错了……spfa也写错了……好吧好像是两个…… 把数轴上的每一个点向它右边的点连一条边,容量为$k$,费用为$0$,然后把每一个区间的左端点向右端点连边, ...