POJ 2472 106 miles to Chicago(Dijstra变形——史上最坑的最长路问题)
题目链接 :
http://poj.org/problem?id=2472
Description
As they are on a mission from God, you should help them find the
safest way to Chicago. In this problem, the safest way is considered to
be the route which maximises the probability that they are not caught.
Input
Each test case starts with two integers n and m (2 <= n <= 100
, 1 <= m <= n*(n-1)/2). n is the number of intersections, m is
the number of streets to be considered.
The next m lines contain the description of the streets. Each street
is described by a line containing 3 integers a, b and p (1 <= a, b
<= n , a != b, 1 <= p <= 100): a and b are the two end points
of the street and p is the probability in percent that the Blues
Brothers will manage to use this street without being caught. Each
street can be used in both directions. You may assume that there is at
most one street between two end points.
The last test case is followed by a zero.
Output
each test case, calculate the probability of the safest path from
intersection 1 (the Palace Hotel) to intersection n (the Honorable
Richard J. Daley Plaza in Chicago). You can assume that there is at
least one path between intersection 1 and n.
Print the probability as a percentage with exactly 6 digits after
the decimal point. The percentage value is considered correct if it
differs by at most 10-6 from the judge output. Adhere to the format
shown below and print one line for each test case.
Sample Input
5 7
5 2 100
3 5 80
2 3 70
2 1 50
3 4 90
4 1 85
3 1 70
0
Sample Output
61.200000 percent
题意描述:
输入顶点数和边的条数以及从无向边,不同的是表示从a到b不被抓住的概率是p
计算并输出从1到n他们不被抓住的最大概率
解题思路:
首先单源最长路问题,Dijstra再合适不过了,但是套模板的话,多半是会错的,因为涉及一点数学概率知识。但是使用求最短路的Flyod算法敲模板也能过。
先说一下Dijstra算法涉及的一点点数学概率知识。
一般来说我们使用Dijstra算法求最短路问题,dis数组存储的是1号顶点到其他各个顶点的直接距离,这个是固有属性,不管之前的点是哪个,只要
要使用该点,那么该点到各个顶点的距离不会因为之前使用的顶点而改变,但是,但是本题求的是不被抓住的最大概率,它由多个事件达成,换句话说,使用事件发生的概率乘法计数原理时,
之前的时间发生会影响后续事件发生的概率。
所以我们初始化dis数组时,应该除了1号顶点置为1以外(因为走到1号顶点的概率是百分之百),其他先置为0,因为只有走到某个顶点了,再将该顶点到其他各顶点的距离进行一个更新。
剩下的就是使用Dijstra算法变形求解最长路啦。
上代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
const int inf=; double map[][];
int main()
{
int n,m,i,j,u,v;
int a,b;
int book[];
double c,p[];
while(scanf("%d",&n), n != )
{
scanf("%d",&m);
memset(map,,sizeof(map));
for(i=;i<=m;i++)
{
scanf("%d%d%lf",&a,&b,&c);
map[a][b]=map[b][a]=c/;
}
memset(book,,sizeof(book));
book[]=;
memset(p,,sizeof(p));
p[]=;
for(i=;i<=n-;i++)
{
u=;
double maxp=;
for(j=;j<=n;j++)
{
if(!book[j] && p[j] > maxp)
{
maxp=p[j];
u=j;
}
}
book[u]=;
for(v=;v<=n;v++)
{
if(p[v] < p[u]*map[u][v])
p[v]=p[u]*map[u][v];
}
}
printf("%.6lf percent\n",p[n]*100.0);
}
return ;
}
再说一下Flyod算法,使用这个算法直接跑的话,因为是最先加入一个点进行松弛,事情也是连续的,也是遵循乘法原理的。
#include<cstdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
using namespace std;
const int inf=; double map[][];
int main()
{
int n,m,i,j,k;
int a,b;
double c;
while(scanf("%d",&n), n != )
{
scanf("%d",&m);
memset(map,,sizeof(map));
for(i=;i<=m;i++)
{
scanf("%d%d%lf",&a,&b,&c);
map[a][b]=map[b][a]=c/;
}
for(k=;k<=n;k++)
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(map[i][j] < map[i][k] * map[k][j])
map[i][j] = map[i][k] * map[k][j];
printf("%.6lf percent\n",map[][n]*);
}
return ;
}
POJ 2472 106 miles to Chicago(Dijstra变形——史上最坑的最长路问题)的更多相关文章
- POJ 2472 106 miles to Chicago
最短路问题变形. 题意是给你一些道路,和路过时不被抓的概率. 要求找一条到达目的地时不被抓的最大概率概率. 初始 dis[]设为 1 .其余为 0 .找最大就可以. #include<cstdi ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- POJ2472106 miles to Chicago
106 miles to Chicago Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3931 Accepted: 1 ...
- POJ 2253 Frogger【最短路变形——路径上最小的最大权】
链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Poj(1797) Dijkstra对松弛条件的变形
题目链接:http://poj.org/problem?id=1797 题意:从路口1运货到路口n,最大的运货重量是多少?题目给出两路口间的最大载重. 思路:j加到s还是接到K下面,取两者的较大者,而 ...
- POJ 3419 Difference Is Beautiful(RMQ变形)
题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...
- poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...
- Poj(2312),坦克大战,BFS的变形
题目链接:http://poj.org/problem?id=2312 挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第 ...
- POJ 2184 Cow Exhibition (01背包的变形)
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
随机推荐
- Express异步进化史
1.导言 在 Javascript 的世界里,异步(由于JavaScript的单线程运行,所以JavaScript中的异步是可以阻塞的)无处不在. Express 是 node 环境中非常流行的Web ...
- C# 实现AOP 的几种常见方式
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的中统一处理业务逻辑的一种技术,比较常见的场景是:日志记录,错误捕获 ...
- linux为用户配置java环境变量
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt226 一. 解压安装jdk 在shell终端下进入jdk-6u14-linu ...
- 七,UDP
那天朋友问我为什么有UDP Sever 和 UDP Client ,,我说:每个人想的不一样,设计上不一样...... 既然是面向无连接的,那么模块发数据就指定IP和端口号,,,为了能和多个UDP ...
- 看懂c/c++ 函数、指针、数组定义
读懂 函数 + 指针 + 数组 c语言运算符机器优先级,看这里 结合运算符优先级,我们试着读懂函数和指针 优先级简单看 表达式提升():一级优先 函数():二级优先 数组[]:二级优先 指针定义*:三 ...
- 团队作业5——测试与发布(Alpha版本)
Deadline: 2017-5-7 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目包括后文的两个方面 测试报告 发布说明 晚交 - 0分 迟交一周以上 - 倒扣本次作业 ...
- 第二次项目冲刺(Beta阶段)--第四天
一.站立式会议照片 二.项目燃尽图 三.项目进展 队员 ID 贡献比 王若凡 201421123022 20% 吕志哲 201421123021 16% 欧阳勇 201421123026 16% 卢 ...
- 201521123065《java程序设计》第七周学习总结
1. 本周学习总结 1.Iterator迭代器用于遍历集合中的元素: 2.使用迭代器删除元素一定要先指向下一个元素在删除第一个元素: 3.List可以有重复对象: Set不能有重复对象: 4.Map是 ...
- 201521123078《Java程序设计》第1周学习总结
1. 本周学习总结 简单的了解JVM,JRE,JDK,编写简单的Java程序 2. 书面作业 为什么java程序可以跨平台运行?执行java程序的步骤是什么?(请用自己的语言书写) 通过JVM虚拟机, ...
- 201521123093 JAVA程序设计
团队博客链接 /[博客链接]http://www.cnblogs.com/yayaya/p/7062197.html 课程设计---购物车系统(201521123093 赵铭) 1.个人负责模块或者任 ...