Domino Effect
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8224   Accepted: 2068

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

解题报告
最后倒下的牌有两情况,1是关键牌。而是两关键之间。
这题的特殊数据是1 0
#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 99999999
using namespace std;
int n,m,mmap[510][510],dis[510],vis[510];
void dij()
{
int i,j,u,minn;
for(i=1; i<=n; i++)
{
dis[i]=mmap[1][i];
vis[i]=0;
}
dis[1]=0;
vis[1]=1;
for(i=0; i<n-1; i++)
{
minn=inf;
u=0;
for(j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
vis[u]=1;
for(j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]>dis[u]+mmap[u][j])
{
dis[j]=dis[u]+mmap[u][j];
}
}
}
}
int main()
{
int i,j,u,v,w,k=1;
while(cin>>n>>m)
{
if(!n&&!m)break; printf("System #%d\n",k++);
if(n==1)
{printf("The last domino falls after 0.0 seconds, at key domino 1.\n\n");
continue;}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
mmap[i][j]=inf;
mmap[i][j]=0;
}
for(i=0; i<m; i++)
{
cin>>u>>v>>w;
if(mmap[u][v]>w)
mmap[u][v]=mmap[v][u]=w;
}
dij();
double maxx1=0;
int u=0;
for(i=1; i<=n; i++)
{
if(dis[i]>maxx1)
{
maxx1=dis[i];
u=i;
}
}
int a1,a2;
double maxx2=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
double t=(dis[i]+dis[j]+mmap[i][j])/2.0;
if(mmap[i][j]<inf&&maxx2<t)
{
maxx2=t;
a1=i;
a2=j;
}
}
}
if(maxx1>=maxx2)
{
printf("The last domino falls after %.1lf seconds, at key domino %d.\n",maxx1,u);
}
else printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",maxx2,a1,a2);
printf("\n");
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ1135_Domino Effect(最短)的更多相关文章

  1. zoj 1298 Domino Effect (最短路径)

    Domino Effect Time Limit: 2 Seconds      Memory Limit: 65536 KB Did you know that you can use domino ...

  2. [POJ] 1135 Domino Effect

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

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

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

  4. GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1)

    GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1) 前言: 无意打开GooglePlay app来着,然后发现首页用了揭示效果,连起来用着感觉还不错. 不清楚 ...

  5. Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果

    Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果 前言: 每次写之前都会来一段(废)话.{心塞...} Google Play首页两个tab背景 ...

  6. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  7. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  8. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  9. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. 华为OJ:查找字符的第一个字符串只出现一次

    您可以使代码有点写得真好,不要直接写双循环,如果,是可能的写函数调用,非常高的可重用性. import java.util.Scanner; public class findOnlyOnceChar ...

  2. Yaha,Yaho

    Yaha: Yaho: 听雪楼上听雪落,雪落无声空余楼. 同样的地方,一坐三年多,人走楼空,回顾空留. 自己非常白痴地画着苹果,非常嗨森地逗自己玩. 这两层精致的书库是大学里面能容纳我的地方(ABC的 ...

  3. nodeJs基础

    Node.js 是一个基于Chrome JavaScript 执行时建立的一个平台, 用来方便地搭建高速的 易于扩展的网络应用· Node.js 借助事件驱动, 非堵塞I/O 模型变得轻量和高效, 很 ...

  4. Linux 多学习过程

    1Linux流程概述 过程是,一旦运行过程中的程序,他和程序本质上的区别.程序是静态的,他奉命收集指令存储在磁盘上. 进程是动态的概念.他是执行者的程序,包括进程的动态创建.调度和消亡,是Linux的 ...

  5. android user如何打开一个版本号root才干

    首先,你要确认你要打开adbd 的root 才干,或者让app 它有可能获得root 才干.   (1). adbd 的root 才干 我们通常debug user 当问题的版本号, 或行为user ...

  6. 人活系列Streetlights (秩)

    人活着系列之Streetlights Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 人活着假设是为了家庭,亲情----能够说是在这个世界上最温暖人心的, ...

  7. mysql1130远程连接没有权限解决方法

    原因擅自在两个远程连接,一种是由于mysql限制,一个是防火墙限制. 1,解决防火墙限制: 于mysql服务主机将关闭防火墙,或者在防火墙高级设置这增加了入站和出站规则,加号mysql的port.同意 ...

  8. java安全性语言

    java通过所谓的沙箱安全模型保证了其安全性,以下我们就来看看java提供的安全沙箱机制. 组成沙箱的基本组件例如以下: 1.类装载器结构: 2.class文件检验器: 3.内置于java虚拟机(及语 ...

  9. VB6基本数据库应用(五):数据的查找与筛选

    同系列的第五篇,上一篇在:http://blog.csdn.net/jiluoxingren/article/details/9633139 数据的查找与筛选 第4篇发布到现在已经过了4天,很抱歉,学 ...

  10. java生成二维码(带logo)

    之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/3947618 ...