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背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
随机推荐
- 快速排序Java实现
package practice; import edu.princeton.cs.algs4.*; public class TestMain { public static void main(S ...
- 【grunt】两小时入门
目录: 1. 用途和场景 2.Grunt插件 3.相关资源 4.环境安装 5.开始学习 5.1 一个新项目 5.2 生成package.json 5.3 在项目中安装grunt和相关插件 5.4 Gr ...
- 工作常用git命令
克隆项目 git clone gitssh地址 提交前的准备 git config user.name 您的中文名 git config user.email 公司邮箱 获取分支 #### 将远端分支 ...
- Linux运维:安装CentOS7.2-图解
矮哥linux运维群: 93324526 笔者QQ:578843228 此篇博文针对最小化安装,和只有图解.有不懂地方,欢迎加群询问.
- BGP基础【第三部】
静态路由的优点:安全稳定.缺点:配置繁琐不灵活.动态路由的优缺点则反之. BGP边界网关路由协议 路径向量(rip是距离矢量) 到达目的网段所要经过的所有as BGP选路不看度量值而参考13种路径属性 ...
- 转: 【Java并发编程】之十八:第五篇中volatile意外问题的正确分析解答(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17382679 在<Java并发编程学习笔记之五:volatile变量修饰符-意料之外 ...
- JavaScript 中运算优先级问题
优先级引发的问题 这篇文章对 JavaScript 中的运算符进行小结,很多人对运算符优先级这一知识点都是一带而过.这就导致在写一些比较奇葩的 js 代码,你并不知道它的输出是啥,下面举一个例子,这也 ...
- sudoku--设想
在查阅了一些资料和自己动手写一写后,找到一种可行的解法. 第一步 首先将9x9的数独方格分成九份3x3的九宫格,如下图 B1 B2 B3 B4 B5 B6 B7 B8 B9 而后在左上角的B1上随机生 ...
- Hyperledger Fabric 1.0 从零开始(五)——运行测试e2e
3:运行测试e2e 3.1.运行fabric-samples的问题说明 该问题说明能够解决6.1.平台特定使用的二进制文件配置第一步的问题.可以选择继续阅读该说明,或者等参考到6.1小节时再反向阅读本 ...
- csv文件读取
from urllib.request import urlopen from io import StringIO import csv data = urlopen("http://py ...