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背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
随机推荐
- JavaScript在应用中的技巧(二)
==,===运算符和强制转换 先看个表达式: "1.0e0" == { valueOf: function() { return true; } }; 是的,这个结果可能有点出乎意 ...
- 安装python虚拟环境
虚拟环境: 之前安装python包的命令: sudo pip3 install 包名包的安装路径:/usr/local/lib/python3.5/dist-packages安装同一个包的不同版本,后 ...
- c# HttpWebRequest 模拟HTTP post 传递JSON参数
//HTTP post JSON 参数 private string HttpPost(string Url, Object ticket) { ...
- Rational Rose2013安装及破解教程
1.下载地址:http://pan.baidu.com/s/1mhKGfHY 2.安装:解压缩文件"[Rational.Rose.Enterprise.Edition.2003].Softw ...
- 软工+C(2017第6期) 最近发展区/脚手架
// 上一篇:工具和结构化 // 下一篇:野生程序员 教育心理学里面有提到"最近发展区"这个概念,这个概念是前苏联发展心理学家维果茨基(Vygotsky)提出的,英文名词是Zone ...
- 【Beta】阶段 第三次Daily Scrum Meeting
每日任务 ·1.本次会议为第三次 Meeting 会议 ·2.本次会议在周三上午9:40召开,会议时间为10分钟 一.今日站立式会议照片 二.每个人的工作(有work item的ID) 三.工作中遇到 ...
- 团队作业8——Beta项目(冲刺计划)
Beta阶段冲刺计划 经过几周的努力我们完成了Alpha的开发,进过一段时间的调整与重组我们继续向Beta版进发. 1. 新成员介绍 林乔桦(201421123074):掌握c语言,JavaScrip ...
- JAVA多线程高并发学习笔记(三)——Callable、Future和FutureTask
为什么要是用Callable和Future Runnable的局限性 Executor采用Runnable作为基本的表达形式,虽然Runnable的run方法能够写入日志,写入文件,写入数据库等操作, ...
- java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
在使用C3P0连接池的时候,发现了这个错误-.原来要使用C3P0的使用,不仅仅要导入c3p0-0.9.2-pre1.jar这个jar包,还要导入mchange-commons-0.2.jar这个jar ...
- ToStringBuilder学习总结
一.简介与引入 1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...