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. ...
随机推荐
- JavaFX横幅类游戏开发 教训 游戏贴图
上一节课,我们即将完成战旗Demo有了一个大概的了解.教训这,我们将学习绘制游戏地图. 由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了. 游戏地图绘 ...
- 在AcGIS随着大数据的生成DEM
在ArcGIS产生DEM时间.数据来源是经常有的高程点.轮廓线,该高程点.等高线密集,可能有几千万.甚至亿高程点.轮廓线. 如果您使用这些矢量数据生成TIN.不能实现的,由于生成TIN时,支持的最大结 ...
- 解决:对 PInvoke 函数的调用导致堆栈不对称问题
解决:对 PInvoke 函数的调用导致堆栈不对称问题 问题描述: 在使用托管代码调用非托管代码时,发生“对 PInvoke 函数“UseTwiHikVisionDllTest!UseTwiHikVi ...
- 【Android进阶】Android程序与JavaScript之间的简单调用
本篇将讲解一个简单的Android与JavaScript之间的简单调用的小程序 效果图 工程结构 HTMLActivity.java代码 package com.example.javatojs; i ...
- SVN命令汇总
1.将文件checkout到本地文件夹 svn checkout path(path是server上的文件夹) 比如:svn checkout svn://192.168.1.1/pro/doma ...
- 我有DIY一Android遥控-所有开源
我有DIY一Android遥控-所有开源 1.试用 记得宋宝华在「设备驱动开发具体解释」提出一个这种理论「软件和硬件互相渗透对方的领地」,这次证明还是确实是这样,使用上层APP软件加上简单的更为简单的 ...
- MongoDB(三)——CRUD
MongoDB作为非关系型数据库.还是传统数据库的增删改查有很大的差别的.这里仅仅是将知识点进行了一下提纲挈领,实际用的时候.我们百度一下具体使用方法就可以. 先看大的几个方面: 一.对于里边的插入和 ...
- hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq
http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...
- 使用hql当异常查询:Xxx is not mapped[from Xxx where ...]
采用当今项目hql询问.出现 QingAoCenterInfo is not mapped[from QingAoCenterInfo where...] 显然地Hibernate映射关系出现了 ...
- T-SQL问题解决集锦——数据加解密(2)
原文:T-SQL问题解决集锦--数据加解密(2) 问题三.如何让指定用户可以对数据表进行Truncate操作? Truncate在对大表全删除操作时,会明显比Delete语句更快更有效,但是因为它不需 ...