POJ1135_Domino Effect(最短)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8224 | Accepted: 2068 |
Description
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
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
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
#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(最短)的更多相关文章
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1)
GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1) 前言: 无意打开GooglePlay app来着,然后发现首页用了揭示效果,连起来用着感觉还不错. 不清楚 ...
- Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果
Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果 前言: 每次写之前都会来一段(废)话.{心塞...} Google Play首页两个tab背景 ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [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 ...
- [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 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- Spark里边:Worker源代码分析和架构
首先由Spark图表理解Worker于Spark中的作用和地位: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW56aHNvZnQ=/font/5a6L ...
- 纯 Swift 封装的 SQLite 框架:SQLite.swift
SQLite.swift 是一个使用纯 Swift 语言封装 SQLite3 的操作框架. 特性: 简单的查询和参数绑定接口 安全.自动类型数据访问 隐式提交和回滚接口 开发者友好的错误处理和调试 文 ...
- android应用框架构建------AppManager
体验一些Android应用程序开发的个人总结 当我们开发应用程序.经常有非常多非常多activity,在这一刻,我们需要一个activity堆栈,以帮助管理activity的finish和start. ...
- RH133读书笔记(6) - Lab 6 Adding New Filesystems to the Filesystem Tree
Lab 6 Adding New Filesystems to the Filesystem Tree Goal: Develop skills and knowlege related to par ...
- slf自己主动绑定实现类过程推断
依照绑定实现类的方式是基于约定原则:推断分下面几个步骤 1.LoggerFactory扫描实现类路径有几个实现类,即在org/slf4j/impl/下有几个StaticLoggerBinder.cla ...
- UVa 10190 - Divide, But Not Quite Conquer!
称号:给你第一个任期的等比数列和倒数公比,最后一个条目假定1这一系列的输出,否则输出Boring!. 分析:数学.递减的.所以公比的倒数一定要大于1.即m > 1. 然后在附加一个条件n &g ...
- Cocos2d-x使用Luajit将Lua脚本编译成bytecode,启用加密
http://www.cocoachina.com/bbs/read.php?tid=205802 lua脚本进行加密,查了一下相关的资料 ,得知lua本身能够使用luac将脚本编译为字节码(byte ...
- MPQ Storm库 源代码分析 一个
MPQ什么? MPQ维基上说的非常明确. 简而言之,它是暴雪公司用于游戏数据打包的工具.星际争霸,魔兽争霸游戏中都有使用.该工具内含游戏资源加密和压缩等功能. git下载地址:http ...
- 杭州电 1203 I NEED A OFFER!
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- WiX Toolset
原文:WiX Toolset 公司换软件打包平台,弄了一个月,Wix toolset的中文资料真的不多,逼着自己看了不少英文资料,终于弄懂了WiX打包的过程,做出了满足要求的安装包 一点基本概念:(F ...