TOJ 1883 Domino Effect
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
#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的更多相关文章
- CF 405B Domino Effect(想法题)
题目链接: 传送门 Domino Effect time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- POJ 1135 Domino Effect(Dijkstra)
点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...
- 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 ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
- POJ 1135 Domino Effect (Dijkstra 最短路)
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9335 Accepted: 2325 Des ...
- POJ 1135.Domino Effect Dijkastra算法
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10325 Accepted: 2560 De ...
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
随机推荐
- Android Studio2.3更换默认的ConstraintLayout布局
1.在as安装目录\plugins\Android\lib\templates\activities\common\root\res\layout下,找到simple.xml.ftl文件 2.用以下布 ...
- HTML、CSS、JavaScript网页制作从入门到精通 (刘西杰) pdf扫描版彩色版
html.css.JavaScript网页制作从入门到精通中从基础知识开始讲起,如html的基本标记.文字与段落标记.表格标记.超链接标记……同时介绍了目前流行的web标准与css网页布局实例,以及基 ...
- Sharepoint2013搜索学习笔记之搜索构架简单概述(一)
Sharepoint搜索引擎主要由6种组件构成,他们分别是爬网组件,内容处理组件,分析处理组件,索引组件,查询处理组件,搜索管理组件.可以将这6种组件分别部署到Sharepoint场内的多个服务器上, ...
- Redis 一个很诡异的问题(部署)
使用Redis并用window服务的方式 运行时.突然报错 在启动进程意外中止 解决方案: 调试了很久 发现居然是在 Logfile的配置中的问题. 错误的logfile logfile " ...
- 选择性的使用 serialize() 进行序列化
serialize 非常方便的帮我们创建 URL 编码文本字符串 输出的字符串格式为 a=1&b=2&c=3 直接可用于Url传参 下面介绍一下选择性的序列化某些标签的使用方法 将 ...
- ECS服务里或者阿里云服务器的二级域名设置方法
我们要实现的效果是,xuxinshuai.abc.com ,具体怎么实现,看下面的流程 第一步:备案域名要有,假如就是www.abc.com 第二步:网站的服务器是IIS的情况下,在部署网站时,需要设 ...
- 六、Note开发工具Visual Studio Code下载安装以及Visual Studio Code的使用
专业的人干专业的事,我们搞Node总不能真的使用文本编辑器傻乎乎的搞吧,文本编辑器来开发Node程序,效率太低,运行Node程序还需要在命令行单独敲命令.如果还需要调试程序,就更加麻烦了.所以我们需要 ...
- GitHub+Hexo 搭建个人网站详细教程
原文链接 GitHub+Hexo 搭建个人网站详细教程 前言: 随着互联网浪潮的翻腾,国内外涌现出越来越多优秀的社交网站让用户分享信息更加便捷.然后,如果你是一个不甘寂寞的程序猿(媛),是否也想要搭建 ...
- 使用IIS服务器部署网页,需要开启服务里的“ASP.NET 状态服务”
否则会提示“请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同.如果服务器位于远程计算机上,请检查 HKEY_LOCAL_MACHI ...
- DRF的认证、权限 和 限制
一.概述 认证是将传入请求与一组标识凭据(例如请求来自的用户或其签名的令牌)相关联的机制.然后 权限 和 限制 组件决定是否拒绝这个请求. 简单来说就是: 认证确定了你是谁 权限确定你能不能访问某个接 ...