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 ...
随机推荐
- centos7的web环境安装配置
1.安装基本东西安装apache yum install httpd安装mariadb yum install mariadb mariadb-server安装php yum install p ...
- 【转】ECharts3.x中的点击事件与行为
在ECharts中主要通过 on 方法添加事件处理函数,ECharts中的事件主要分为两种,1)鼠标事件,在鼠标click or hove 时触发鼠标事件: 2)另外一种是在ECharts在做图形 ...
- 开启saltstack的web界面
saltstack官方有提供一个web界面叫halite,halite是用cherrypy web框架开发的,连接后端的saltstack api,web界面虽然简单点,但功能还算齐全,今天就来开启s ...
- (10/24) 图片跳坑大战--处理html中的图片
补充,在前面的服务启动执行命令中,我们在package.json中的配置信息为: "scripts": { "server": "webpack-de ...
- FlexPaper及二次开发
Flexpaper二次开发入门教程 http://ajava.org/course/web/?page=2
- elk6.22
启动错误: 参考网站:https://blog.csdn.net/feinifi/article/details/73633235?utm_source=itdadao&utm_medium= ...
- 在spring引入log4j(非web项目)
https://blog.csdn.net/u012578322/article/details/78012183 在spring中使用log4j 引入log4j软件包 配置log4j属性 加载log ...
- Hibernate hql 多表查询
String hql="select c from Col c ,UserRole role where c.id=role.columnId and c.id=? and role.use ...
- Recursion递归
/*java.lang 核心包 如 String Math Integer System Thread等 拿来直接用 * java.awt 窗口工具 GUI * java.net 网络包 * java ...
- 数据库索引&数据页
索引的好处 索引带来的益处可能很多读者会认为只是"能够提高数据检索的效率,降低数据库的IO成本". 确实,在数据库中表的某个字段创建索引,所带来的最大益处就是将该字段作为检索条件时 ...