题目链接:

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的更多相关文章

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

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

  2. POJ 1135 Domino Effect (Dijkstra 最短路)

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 Des ...

  3. POJ 1135.Domino Effect Dijkastra算法

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 De ...

  4. 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 ...

  5. [POJ] 1135 Domino Effect

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

  6. POJ 1135 Domino Effect(Dijkstra)

    点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...

  7. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  8. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  9. UVA211-The Domino Effect(dfs)

    Problem UVA211-The Domino Effect Accept:536  Submit:2504 Time Limit: 3000 mSec  Problem Description ...

  10. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

随机推荐

  1. sqlite语句主页

    因为现在android手机用sqlite数据,但是sql语句很多和sqlserver不同..所以还是把官网记下以便开发:http://www.sqlite.org/lang.html

  2. Dependency Walker的替代品Dependencies

    在c++时代, Dependency Walker基本上是大部分程序员必备的工具之一,很可惜的是从2006起就不更新了.而且只支持vc的名字undemangle, https://github.com ...

  3. 解决Hash碰撞冲突方法总结

    Hash碰撞冲突 我们知道,对象Hash的前提是实现equals()和hashCode()两个方法,那么HashCode()的作用就是保证对象返回唯一hash值,但当两个对象计算值一样时,这就发生了碰 ...

  4. Leetcode 35——Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. Beta No.7

    今天遇到的困难: 构造新适配器的时候出现了某些崩溃的问题 ListView监听器有部分的Bug 今天完成的任务: 陈甘霖:完成相机调用和图库功能,完成阿尔法项目遗留下来的位置调用问题,实现百度定位 蔡 ...

  6. 20162311 实验三 敏捷开发与XP实践 实验报告

    20162311 实验三 敏捷开发与XP实践 实验报告 实验内容 一.研究学习IDEA中的Code菜单 使用Code ->Reformate Code功能将以下代码格式化 public clas ...

  7. Java Collections API和泛型

    Java Collections API和泛型 数据结构和算法 学会一门编程语言,你可以写出一些可以工作的代码用计算机来解决一些问题,然而想要优雅而高效的解决问题,就要学习数据结构和算法了.当然对数据 ...

  8. PTA博客制作的模版

    C高级第 次PTA作业( ) 题目 - 此处填写题目名称 1.设计思路 (1)算法 (2)流程图 2.实验代码 此处填写代码 3.本题调试过程碰到问题及解决办法 错误信息: 错误原因: 改正方法: 提 ...

  9. scrapy 模拟登陆

    import scrapy import urllib.request from scrapy.http import Request,FormRequest class LoginspdSpider ...

  10. Django-rest-framework源码分析----权限

    添加权限 (1)API/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # utils/permission.py class SVIPPr ...