POJ 1135.Domino Effect Dijkastra算法
Domino Effect
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10325 | Accepted: 2560 |
Description
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 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
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. 题目链接:http://poj.org/problem?id=1135
题目意思:输入n,m表示有n张关键牌,m表示n张牌之间有m行普通牌进行连接。n张牌的编号为1~n。每两张关键牌之间之多只有一行普通牌,并且图案是联通的。从第1张关键牌推到。最后倒下的如果是关键牌,输出看样例;如果是普通牌,输出看样例。
思路:因为起点固定,可以用求最短路径的Dijkastra算法(也可以用BFS搜索,如果时间更少就入列)。求出每张关键牌的倒下时间。两个关键牌i,j之间的普通牌完全倒下的时间为(edge[i][j]+time[i]+time[j])*1.0/2.0;如果时间不是等于time[i]或者time[j],时间就是这行普通牌最后倒下的时间;否则最后倒下的牌就是关键牌;
代码:
#include<iostream>
#include<cstdio>
using namespace std;
#define INF 10000000
int n,m;
int edge[][];
int sign[],time[];
void Init()
{
int i,j;
int u,v,w;
for(i=; i<=n; i++)
{
time[i]=INF;
sign[i]=;
for(j=; j<=n; j++)
edge[i][j]=INF;
}
for(i=; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]=edge[v][u]=w;
}
}
void Dijkstra(int v0)
{
int i,j,t;
time[v0]=;
for(i=; i<n; i++)
{
sign[v0]=;
int Min=INF;
for(j=; j<=n; j++)
{
if(edge[v0][j]<INF&&time[v0]+edge[v0][j]<time[j])
time[j]=time[v0]+edge[v0][j];
if(sign[j]==&&time[j]<Min)
{
Min=time[j];
t=j;
}
}
v0=t;
if(v0<=&&v0>n) break;
}
}
int main()
{
int i,j;
int t=;
while(scanf("%d%d",&n,&m)&&!(n==&&m==))
{
Init();
Dijkstra();
float Max1=;
int flag=;
for(i=; i<=n; i++)
if(time[i]>Max1)
{
Max1=time[i];
flag=i;
}
float Max2=;
int a=,b=;
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
if(edge[i][j]<INF)
{
float ans=(edge[i][j]+time[i]+time[j])*1.0/;
if(ans>Max2)
{
Max2=ans;
a=i;
b=j;
}
}
}
cout<<"System #"<<t<<endl;
if(Max2>Max1) printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",Max2,a,b);
else printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",Max1,flag);
t++;
}
return ;
}
POJ 1135.Domino Effect Dijkastra算法的更多相关文章
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- POJ 1135 Domino Effect (Dijkstra 最短路)
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9335 Accepted: 2325 Des ...
- 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 ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- CF 405B Domino Effect(想法题)
题目链接: 传送门 Domino Effect time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- 1135: 零起点学算法42——多组测试数据(求和)IV
1135: 零起点学算法42--多组测试数据(求和)IV Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
随机推荐
- mysql更新(五) 完整性约束 外键的变种 三种关系 数据的增删改
11-数据的增删改 本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识. ...
- 20165233 2017-2018-2 《Java程序设计》第九周学习总结
20165233 2017-2018-2 <Java程序设计>第九周学习总结 教材学习内容总结 基础 - URL类:java.net包中的URL类是对统一资源定位符的抽象,使用URL创建对 ...
- ANA网络分析
ANN网络分析 Mnist手写数字识别 Mnist数据集可以从官网下载,网址: http://yann.lecun.com/exdb/mnist/ 下载下来的数据集被分成两部分:55000行的训练数据 ...
- hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果
hammer.js 的使用. (手机手势插件) 捏合.捏开.图片放大 的一个手机图片“放大缩小可拖动”的小效果. 相关js 到 http://www.bootcdn.cn/ 查找和下载. hamme ...
- IE6部分兼容问题
border-style:dotted 点线 IE6不兼容 (除了solid以外,其它都有兼容问题,不完全一样) a IE6 不支持a以外的所有标签伪类,IE6以上版本支持所有标签的hover伪类. ...
- C++ 11 中的 Lambda 表达式的使用
Lambda在C#中使用得非常频繁,并且可以使代码变得简洁,优雅. 在C++11 中也加入了 Lambda. 它是这个样子的 [] () {}... 是的三种括号开会的节奏~ [] 的作用是表示La ...
- dreamwave基础
WEBcs架构需要在客户段安装程序, 需要安装程序, 工作量会比较大, 需要安装和维护, 比如以后系统升级, 会很麻烦. 优点是一些业务逻辑可以在客户端, 可以减少服务器的一些压力, 客户端的界面操作 ...
- 快速上手Vue
课程目录: ES6常用语法 Vue基础以及指令 Vue组件 Vue-Router Vue生命周期 npm webpack vue-cli Vuex以及axios
- 第八章 高级搜索树 (xa3)红黑树:插入
- nyoj86-找球号(一) 【set 二分查找 hash】
http://acm.nyist.net/JudgeOnline/problem.php?pid=86 找球号(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...