POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
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.
题目大意:给你n个关键的多米诺骨牌,这n个关键的多米诺骨牌由m条由骨牌组成的“路”相连,每条路都有自己的“长度”,当这n个骨牌中的任意一个骨牌 k 倒塌时,与k相连的所有“路”上的骨牌也会随之而倒,让你求把骨牌 1 推到后,所有骨牌中最后一个倒塌的骨牌距离骨牌1的最短距离。
解题思路:题目中保证图是连通的,我们可以先求出骨牌1到其他(n - 1)个关键骨牌的最短距离,得到这些距离中的最大值MAX,然后枚举图中的每条边,再更新MAX,具体详解请看程序:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std ;
int n , m ;
const int MAXN = 505 ;
struct Node
{
int adj ;
double dis ;
};
const int INF = 0x7fffffff ;
int t ;
vector<Node> vert[MAXN] ;
double d[MAXN] ; // 保存顶点 1 到其他(n - 1)个顶点的最短距离
void clr() // 初始化
{
int i ;
for(i = 0 ; i < MAXN ; i ++)
vert[i].clear() ;
memset(d , 0 ,sizeof(d)) ;
}
void init()
{
clr() ;
int i , j ;
Node tmp ;
for(i = 0 ; i < m ; i ++) // 用邻接表建图
{
int a , b ;
double c ;
scanf("%d%d%lf" , &a , &b , &c) ; tmp.adj = b ;
tmp.dis = c ;
vert[a].push_back(tmp) ; tmp.adj = a ;
tmp.dis = c ;
vert[b].push_back(tmp) ;
}
}
queue<int> q ;
bool inq[MAXN] ;
void spfa(int u) // 求最短路
{
while (!q.empty())
q.pop() ;
q.push(u) ;
inq[u] = true ;
d[u] = 0 ;
int tmp ;
Node v ;
while (!q.empty())
{
tmp = q.front() ;
q.pop() ;
inq[tmp] = false ;
int i ;
for(i = 0 ; i < vert[tmp].size() ; i ++)
{
v = vert[tmp][i] ;
if(d[tmp] != INF && d[tmp] + v.dis < d[v.adj])
{
d[v.adj] = d[tmp] + v.dis ;
if(!inq[v.adj])
{
q.push(v.adj) ;
inq[v.adj] = true ;
}
}
}
}
}
void solve()
{
memset(inq , 0 , sizeof(inq)) ;
int i , j ;
for(i = 1 ; i <= n ; i ++)
{
d[i] = INF ;
}
spfa(1) ;
double MAX = d[1] ;
int MAXb = 1 ;
for(i = 1 ; i <= n ; i ++)
{
if(MAX < d[i])
{
MAX = d[i] ;
MAXb = i ;
}
}
int pan = 0 ;
int t1 , t2 ;
for(i = 1 ; i <= n ; i ++) // 枚举每条边 , 更新MAX
{
for(j = 0 ; j < vert[i].size() ; j ++)
{
Node tn = vert[i][j] ;
int ta = tn.adj ;
double td = tn.dis ;
if((d[i] + d[ta] + td) / 2 > MAX ) // 注意:最大距离的求法
{
pan = 1 ;
MAX = (d[i] + d[ta] + td) / 2;
if(i < ta)
{
t1 = i ;
t2 = ta ;
}
else
{
t1 = ta ;
t2 = i ;
}
}
}
}
printf("The last domino falls after %.1f seconds," , MAX) ;
if(pan)
{
printf(" between key dominoes %d and %d.\n" , t1 , t2) ;
}
else
{
printf(" at key domino %d.\n" , MAXb) ;
}
puts("") ;
}
int ca ;
int main()
{
ca = 0 ;
while (scanf("%d%d" , &n , &m) != EOF)
{
if(n == 0 && m == 0)
break ;
init() ;
printf("System #%d\n" , ++ ca) ;
solve() ;
}
return 0 ;
}
POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang的更多相关文章
- 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 Dijkastra算法
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10325 Accepted: 2560 De ...
- POJ 1135 Domino Effect(Dijkstra)
点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...
- [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 ...
- TOJ 1883 Domino Effect
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 ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
随机推荐
- SQL Server 的 6 种隔离级别
背景知识: 高并发一直以来是数据的所追求的目标,然,一般事物是有两面性的.不多说了,等下变邪教了.下面直接看 并发性最高的隔离级别 read uncommitted 1. read uncommitt ...
- 再次优化NGINX+php-fpm上传
上次写了一篇nginx+php-fpm优化上传,一位博友留言介绍了,第三方nginx upload module http://www.grid.net.ru/nginx/upload.en.html ...
- USB Mass Storage协议分析
目录 简介 指令数据和状态协议 CBW指令格式 CSWCommand Status Wrapper状态格式 SCSI命令集 Format Unit Inquiry MODE SELECT 简介 USB ...
- Windows消息拦截技术的应用(作者博客里有许多相关文章)
民航合肥空管中心 周毅 一.前 言 众所周知,Windows程式的运行是依靠发生的事件来驱动.换句话说,程式不断等待一个消息的发生,然后对这个消息的类型进行判断,再做适当的处理.处理完此次消息后又回到 ...
- how to remove MouseListener / ActionListener on a JTextField
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new j ...
- C语言的本质(25)——C标准库之内存管理
程序中需要动态分配一块内存时怎么办呢?我们可以定义一个缓冲区数组,但是这种方法不够灵活,C89要求定义的数组是固定长度的,而程序往往在运行时才知道要动态分配多大的内存,例如: void foo(cha ...
- Error:/bin/bash: /bin/java: No such file or directory
描述:在Hadoop运行Job的时候,可能会报这样的一个错误“/bin/bash: /bin/java: No such file or directory”,那是因可能有些地方用到了/bin/jav ...
- javascript 事件处理
[写在前面]近期一直在看js的基础,毕竟jquery尽管好用,总归是用着别人写的,仅仅会用api不如搞清楚实现的原理. 等把js基础巩固好了一定要去读jquery的源代码. 事件流 事件流描写叙述的是 ...
- Android appcompat备案
使用Eclipse创建Android项目,project多出appcompat_v7,此情况在ADT升级到22.6.x版本后出现,22.3.x前版本不存在.此项为了实现向下兼容sdk的功能. 点击项目 ...
- 【最大点权独立集】【HDU1565】【方格取数】
题目大意: 给你一个n*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大. 初看: 没想法 ...