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]. 一些口胡: ...
随机推荐
- 快速排序及优化(Java实现)
普通快速排序 找一个基准值base,然后一趟排序后让base左边的数都小于base,base右边的数都大于等于base.再分为两个子数组的排序.如此递归下去. public class QuickSo ...
- 笔记:Spring Cloud Eureka 服务治理
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
- oracle 常用sql字符函数介绍
常用字符函数介绍 1.ascii 返回与指定的字符对应的十进制数: SQL>select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') ...
- 03-第一个脚本程序以及输入输出_Python编程之路
上节课已经教大家安装了Python的解释器,那么这节课我们就可以正式来写代码了 说明:在下面的代码演示中,我将大部分使用python交互器演示代码的输入输出,注意">>>& ...
- Alpha冲刺——Day2
一.合照 二.项目燃尽图 三.项目进展 图形界面基本完成 接口文档框架完成,接下来将会不断细化填充 登录界面向服务器请求数据进行ing 四.明日规划 1.注册登录接口能够完成 2.研究idea实现获得 ...
- C语言第一周作业
题目一:7-3 温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码 2.设计 ...
- Hibernate学习错误集锦-GenericJDBCException: could not execute statement
初次使用Hibernate,进行junit测试,报如下错误. 原因:Hibernate帮我们管理主键了,我们不需要对主键赋值,并且主键是自增的.所以在数据库中,逐渐选项应当勾选 org.hiberna ...
- choose the max from numbers, use scanf and if else (v1:21.9.2017,v2:23.9.2017)
#include<stdio.h> int main(){ int a,b,c,max; printf("请输入一个数值: "); scanf("%d&quo ...
- Linux 复习
shift + control + + 终端窗口放大 control + - 终端窗口缩小 ls -alh > laowang.txt 重定向,并覆盖源文件内容 ls -alh >& ...
- Flask 学习 十二 用户评论
评论在数据库中的表示 由于评论和2个模型有关系,分别是谁发了评论,以及评论了哪个文章,所以这次要更新数据库模型 models.py 创建用户评论数据库模型 class Comment(db.Model ...