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

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.

Source

Southwestern European Regional Contest 1996



题目链接:http://poj.org/problem?id=1135



题目大意:有n张关键的多米诺骨牌,m条路。从一条路的起点到终点的牌所有倒下须要时间t。计算最后一张倒下的牌在哪。是什么时候



题目分析:两种情况:

1.最后倒下的牌就是某张关键牌,则时间为最短路中的最大值ma1

2.最后倒下的牌在某两张牌之间,则时间为到两张牌的时间加上两张牌之间牌倒下的时间除2的最大值ma2

最后比較m1和m2。若m1大则为第一种情况,否则是另外一种情况



一组例子:

4 4

1 2 5

2 4 6

1 3 5

3 4 7

0 0



答案:

The last domino falls after 11.5 seconds, between key dominoes 3 and 4.

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 505;
int map[MAX][MAX];
int dis[MAX];
bool used[MAX];
int n, m, ca = 1; void Dijkstra(int v0)
{
memset(used, false, sizeof(used));
for(int i = 1; i <= n; i++)
dis[i] = map[v0][i];
used[v0] = true;
dis[v0] = 0;
for(int i = 0; i < n - 1; i++)
{
int u = 1, mi = INF;
for(int j = 1; j <= n; j++)
{
if(!used[j] && dis[j] < mi)
{
mi = dis[j];
u = j;
}
}
used[u] = true;
for(int k = 1; k <= n; k++)
if(!used[k] && map[u][k] < INF)
dis[k] = min(dis[k], dis[u] + map[u][k]);
}
double ma1 = -1, ma2 = -1;
int pos, pos1, pos2;
for(int i = 1; i <= n; i++)
{
if(dis[i] > ma1)
{
ma1 = dis[i];
pos = i;
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(map[i][j] < INF && (dis[i] + dis[j] + map[i][j]) / 2.0 > ma2)
{
ma2 = (dis[i] + dis[j] + map[i][j]) / 2.0;
pos1 = i;
pos2 = j;
}
}
}
if(ma1 < ma2)
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n", ma2, pos1, pos2);
else
printf("The last domino falls after %.1f seconds, at key domino %d.\n\n", ma1, pos); } int main()
{
while(scanf("%d %d", &n, &m) != EOF && (n + m))
{
for(int i = 1; i <= n; i++)
{
dis[i] = INF;
for(int j = 1; j <= n; j++)
map[i][j] = INF;
}
for(int i = 0; i < m; i++)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
map[u][v] = w;
map[v][u] = w;
}
printf("System #%d\n", ca ++);
Dijkstra(1);
}
}

POJ 1135 Domino Effect (Dijkstra 最短路)的更多相关文章

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

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

  2. POJ 1135 Domino Effect(Dijkstra)

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

  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 (spfa + 枚举)- from lanshui_Yang

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

  5. [POJ] 1135 Domino Effect

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

  6. poj 2253 Frogger (dijkstra最短路)

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  7. POJ 2253 Frogger(dijkstra 最短路

    POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

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

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

  9. POJ. 2253 Frogger (Dijkstra )

    POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...

随机推荐

  1. 使用jstl方式替换服务器请求地址

    <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>

  2. 当Python与数模相遇

    数模有一个题目要处理杭州自行车在每个站点可用数量和已经借出数量,这数据在www.hzbus.cn上可以获取,它是10分钟更新一次的.这些数据手动获取,需要不停的刷页面,从6:00am到9:00pm,显 ...

  3. CentOS下Lua 环境的搭建

    curl -R -O http://www.lua.org/ftp/lua-5.2.2.tar.gz .tar.gz cd lua- make linux test 报错 cd src &&a ...

  4. laravel5 session的基本使用

    配置session配置文件位于config/session.hpp 默认情况下使用session驱动为文件驱动,在生产环境中,建议使用memcache或者redis驱动以便获取更快的session性能 ...

  5. jquery下载,实时更新jquery1.2到最新3.3.1所有版本下载

    描述:jquery下载,实时更新jquery1.2到最新3.3.1所有版本下载 https://www.jb51.net/zt/jquerydown.htm (注意:jquery-2.0以上版本不再支 ...

  6. 洛谷P3964 [TJOI2013]松鼠聚会 [二分答案,前缀和,切比雪夫距离]

    题目传送门 松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以用一个点x,y表示, ...

  7. 002.FTP配置项详解

    一 相关配置项 anonymous_enable=YES #允许匿名用户登录 local_enable=YES #允许本地用户登录 write_enable=YES #允许本地用户上传 local_u ...

  8. KVM源代码阅读--内核版本3.17.4

    为了更加深入的学习虚拟化,因此我必须把KVM源代码搞清楚,这是一个必须要挖的坑.我会把自己的一些阅读的代码贴上来,可能会有理解不对的地方,希望和大家一起交流,请多提意见,以便于纠正错误.所用的内核版本 ...

  9. Xtreme9.0 - Light Gremlins 容斥

    Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...

  10. AutoFac简单入门

    AutoFac是.net程序下一个非常灵活易用,且功能强大的DI框架,本文这里简单的介绍一下使用方法. 安装: Install-Package Autofac 简单的示例: static void M ...