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.
   题目大意:给你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的更多相关文章

  1. POJ 1135 -- Domino Effect(单源最短路径)

     POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...

  2. POJ 1135 Domino Effect (Dijkstra 最短路)

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 Des ...

  3. POJ 1135.Domino Effect Dijkastra算法

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 De ...

  4. POJ 1135 Domino Effect(Dijkstra)

    点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...

  5. [POJ] 1135 Domino Effect

    Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...

  6. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  7. TOJ 1883 Domino Effect

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  8. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  9. UVA211-The Domino Effect(dfs)

    Problem UVA211-The Domino Effect Accept:536  Submit:2504 Time Limit: 3000 mSec  Problem Description ...

随机推荐

  1. Qt之QTemporaryFile(文件名唯一,且可以自动删除)

    简述 QTemporaryFile类是操作临时文件的I/O设备. QTemporaryFile用于安全地创建一个独一无二的临时文件.临时文件通过调用open()来创建,并且名称是唯一的(即:保证不覆盖 ...

  2. 什么是JS事件冒泡

    什么是JS事件冒泡? 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个 ...

  3. poj 1065 Wooden Sticks_贪心

    题意:将木棍放在机器里处理,第一根需要一分钟,剩余的如果大于等于前边放入的长度和重量,就不用费时间,否则需要一分钟,计算给出一组数的最少时间. 思路:先按长度排序,相同在比较重量,然后按顺序比较得出结 ...

  4. sql 中的 indexOf 与 lastIndexOf

    DECLARE @Name NVARCHAR (50)SET @Name = '12345.67890ABCDE.FGHIJKLMNOPQRSTUVWXYZTest' DECLARE @Positio ...

  5. Python学习入门基础教程(learning Python)--5 Python文件处理

    本节主要讨论Python下的文件操作技术. 首先,要明白为何要学习或者说关系文件操作这件事?其实道理很简单,Python程序运行时,数据是存放在RAM里的,当Python程序运行结束后数据从RAM被清 ...

  6. and then set HOMEBREW_GITHUB_API_TOKEN.

    andyMacBook-Pro:~ andy$ brew search redis hiredis   redis homebrew/nginx/redis2-nginx-module Error: ...

  7. JavaScript创建类的方式

    一些写类工具函数或框架的写类方式本质上都是 构造函数+原型.只有理解这一点才能真正明白如何用JavaScript写出面向对象的代码,或者说组织代码的方式使用面向对象方式.当然用JS也可写出函数式的代码 ...

  8. golang高级部分

    一.golang之OOP(orient object programming) 在函数声明时, 在其名字之前放上一个变量, 即是一个方法. 这个附加的参数会将该函数附加到这种类型上, 即相当于为这种类 ...

  9. Oracle 启用块跟踪

    Oracle 启用块跟踪,语法示例如下: alter database enable block change tracking using file '/u01/app/oracle/oradata ...

  10. Hadoop学习笔记——入门指令操作

    假设Hadoop的安装目录HADOOP_HOME为/home/admin/hadoop. 启动与关闭启动HADOOP1. 进入HADOOP_HOME目录. 2. 执行sh bin/start-all. ...