POJ-1135 Domino Effect---最短路Dijk
题目链接:
https://vjudge.net/problem/POJ-1135
题目大意:
有N个关键的多米诺骨牌,这些牌通过一些路径相连接,这些路径是由一排其他骨牌构成的。已知每一条路径上的骨牌倒下需要的时间。现在从把编号为1的关键骨牌推倒,问多长时间后所有的骨牌(包括关键牌和它们之间的路径上的骨牌)都倒下,时间精确到小数点后一位。
思路:
先用dijkstra算法计算每一张骨牌倒下的时间d[i],然后取最大值time1。
再每两张骨牌之间全部倒下的时间的最大值time2 = max{(d[i] + d[j] + Map[i][j]) / 2}
如果time1>=time2,那就是在某张关键牌处结束,不然则在两张关键牌中结束
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = << ;
int T, n, m, cases;
int Map[maxn][maxn];
bool v[maxn];
int d[maxn];
void dijkstra(int u0)
{
memset(v, , sizeof(v));
for(int i = ; i <= n; i++)d[i] = (i == u0 ? : INF);
for(int i = ; i <= n; i++)
{
int x, m = INF;
for(int j = ; j <= n; j++)if(!v[j] && d[j] <= m)m = d[x = j];
v[x] = ;
for(int j = ; j <= n; j++)
d[j] = min(d[j], d[x] + Map[x][j]);//松弛操作
}
}
int main()
{
while(cin >> n >> m)
{
if(!n && !m)break;
for(int i = ; i <= n; i++)for(int j = ; j <= n; j++)Map[i][j] = INF;
int x, y, z;
for(int i = ; i < m; i++)
{
cin >> x >> y >> z;
Map[x][y] = Map[y][x] = z;
}
dijkstra();
double mintime1 = -INF, mintime2 = -INF;
int ans, ans1, ans2;
for(int i = ; i <= n; i++)
{
if(mintime1 < d[i])
{
mintime1 = d[i];
ans = i;
}
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(Map[i][j] == INF)continue;
if(mintime2 < 1.0 * (d[i] + d[j] + Map[i][j]) / 2.0)
{
mintime2 = 1.0 * (d[i] + d[j] + Map[i][j]) / 2.0;
ans1 = i, ans2 = j;
}
}
}
printf("System #%d\n", ++cases);
if(mintime1 >= mintime2)printf("The last domino falls after %.1f seconds, at key domino %d.\n\n", mintime1, ans);
else printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n", mintime2, ans1, ans2);
}
}
POJ-1135 Domino Effect---最短路Dijk的更多相关文章
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- POJ 1135 Domino Effect (Dijkstra 最短路)
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9335 Accepted: 2325 Des ...
- POJ 1135.Domino Effect Dijkastra算法
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10325 Accepted: 2560 De ...
- 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 ...
- [POJ] 1135 Domino Effect
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12147 Accepted: 3046 Descri ...
- POJ 1135 Domino Effect(Dijkstra)
点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- CF 405B Domino Effect(想法题)
题目链接: 传送门 Domino Effect time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
随机推荐
- [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法
最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...
- poj-1131-(大数)八进制转化成十进制
Description Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For e ...
- Linux 如何使用echo指令向文件写入内容
0.前言 本文总结如何使用echo命令向文件中写入内容,例如使用echo指令覆盖文件内容,使用echo指令向文件追加内容,使用echo指令往文件中追加制表符. echo向文件中输出内容 ...
- MySQL详解--锁,事务
http://www.cnblogs.com/jukan/p/5670950.html http://blog.csdn.net/xifeijian/article/details/20313977 ...
- java中有关流操作的类和接口
一.java操作l流有关的类和接口 1.File 文件类 2.RandomAccessFile 随机存储文件类 3.InputStream 字节输入流 4.OutputStream 字节输出流 5.R ...
- Python3 的描述符--完整例子详细解释
##描述符类的例子,这个例子说明了描述符和被描述符之间的关系 ##摄氏温度 class Celsius(): ## 1 描述符类 def __init__(self,value = 26.0): ## ...
- 后台返回null iOS
1.第一种解决方案 就是在每一个 可能传回null 的地方 使用 if([object isEqual:[NSNUll null]]) 去判断 2.第二种解决方案 网上传说老外写了一个Categor ...
- bug终结者 团队作业第八周
bug终结者 团队作业第八周 本次任务 素材提供及编辑:20162328 蔡文琛 博客修改完善:20162322 朱娅霖 "bug终结者" 宏伟蓝图 UML 手绘底稿 用例图 选项 ...
- 关于搭建MyBatis框架(二)
由于在[关于使用Mybatis的使用说明(一)http://www.cnblogs.com/zdb292034/p/8675766.html]中存在不太完善地方,通过此片文档进行修订: 阅读指南:(1 ...
- 07-TypeScript的For循环
在传统的JavaScript中,关于循环,可以有两种方式,一种是forEach,一种是for. forEach的用法如下: var sarr=[1,2,3,4]; sarr.desc="he ...