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背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
随机推荐
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
- httpd2.4常用配置
author:JevonWei 版权声明:原创作品 httpd 2.4配置 切换使用的MPM Centos7:/etc/httpd/conf.modules.d/00-mpm.conf 启用要启用的M ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...
- JVM中锁优化,偏向锁、自旋锁、锁消除、锁膨胀
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt364 本文将简单介绍HotSpot虚拟机中用到的锁优化技术. 自旋锁 互斥同 ...
- yum安装mariadb-galera同步
节点 ip地址 hostname 系统版本 程序版本 node1 10.4.90.90 mysql1 db1 ...
- Linux-mknod命令(9)
mknod命令用于创建字符设备文件和块设备文件 (ls /dev -l 结果显示b开头和c开头的,即标识了块设备和字符设备.) 为了管理设备,所以设备中都有两个设备号: 主设备号:为了区分不同类型的 ...
- Swiper 滑动
1.http://www.swiper.com.cn/download/ 下载Swiper.JS Swiper.CSS 2.引入项目,添加html <div class="cont ...
- Java中死锁的简单例子及其避免
死锁:当一个线程永远地持有一个锁,并且其他线程都尝试获得这个锁时,那么它们将永远被阻塞.比如,线程1已经持有了A锁并想要获得B锁的同时,线程2持有B锁并尝试获取A锁,那么这两个线程将永远地等待下去. ...
- JS嵌套循环的典型练习题
1.斐波那契数列 ①分数 <script type="text/javascript"> var a = 1 var b = 1 var c for ...
- 教程,Python图片转字符堆叠图
Python 图片转字符画 一.实验说明 1. 环境登录 无需密码自动登录, 2. 环境介绍 本实验环境采用带桌面的UbuntuLinux环境,实验中会用到桌面上的程序: LX终端(LXTermina ...