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 ...
随机推荐
- RowToColumn
SELECT S.NAME, sum(decode(S.COURSE,'语文',S.SCORE,0))"语文", sum(decode(S.COURSE,'数学',S.SCORE, ...
- C基本语句和运算符
1,逗号运算符
- python入门-用户输入
1 input()函数来实现用户输入,程序在等待输入的时候会终止,获取用户的输入后继续 message = input("tell me something,and I will repre ...
- 模拟Vue之数据驱动1
一.前言 Vue有一核心就是数据驱动(Data Driven),允许我们采用简洁的模板语法来声明式的将数据渲染进DOM,且数据与DOM是绑定在一起的,这样当我们改变Vue实例的数据时,对应的DOM元素 ...
- leetcode268
public class Solution { public int MissingNumber(int[] nums) { var list = nums.OrderBy(x => x).To ...
- CSS 3栏自适应布局
绝对定位 css html,body{margin: 0px;height:100%;} div{height: 100%;} .left,.right {top: 0px;position: abs ...
- Delphi 变体数组 Dataset Locate 查找定位
Format 函数 Delphi 支持“开参数”和动态数组,变体数组,使用时的语法类似 Delphi 中的集合:采用两个方括号把不同类型的变量括起来(这太方便了啊),也可以采用声明一个 TVarRec ...
- X86汇编概要
来自:https://www.cnblogs.com/jiftle/p/8453106.html 本文翻译自:http://www.cs.virginia.edu/~evans/cs216/guide ...
- 提取linux中eth0的IP地址
法1:cut [root@oldboy oldboy]# ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " & ...
- 【转】oracle 体系结构
前几天面试的时候面试官才问过我Oracle的体系结构,让我在一张白纸上画出来.回头想想当时答得还不错,大部分内容都描述出来了,呵呵,刚才在网上看到一篇讲解ORACLE体系结构的文章,觉得不错,转过来存 ...