URAL 1934 spfa算法
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Jack: Any idea when Jones might release said terrible beastie?
Bootstrap: I already told you, Jack. Your time is up. It
comes now, drawn with ravenous hunger to the man what bears the black
spot.
going to high seas because sea monster Kraken is waiting there for him.
But he can’t stay in his place due to his freedom-loving nature. And now
Jack is going to Tortuga.
islands in the Caribbean Sea. Jack is going to reach Tortuga, sailing
from island to island by routes that allow him to be in the high seas
for a short time. Jack knows such routes for some pairs of islands, but
they could also be dangerous for him. There is a probability to meet
Kraken on each route.
number of islands as possible. If there are several variants of such
paths he wants to choose a path with the least probability of meeting
Kraken.
But Jack will be satisfied with any path with minimal number of
islands if the probability of meeting Kraken on this path differs from
the minimal one in no more than 10−6. Help Jack find such path.
Input
m — the quantity of islands and known routes between them (2 ≤
n ≤ 10 5; 1 ≤
m ≤ 10 5). The second line contains two integers s and t — the number of island where Jack is and the number of Tortuga (1 ≤
s,
t ≤
n;
s ≠
t). Each of the following m lines contains three integers — the numbers of islands ai and bi where the route is known and pi — probability to meet Kraken on that route as percentage (1 ≤
ai,
bi ≤
n;
ai ≠
bi; 0 ≤
pi ≤ 99). No more than one route is known between each pair of islands.
Output
Sample Input
| input | output |
|---|---|
4 4 |
3 0.19 |
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=;
int n,m;
bool vis[maxn];
int before[maxn],dis[maxn];
const int inf=;
int first[maxn];
int cnt; double tp[maxn]; struct node
{
int v;
double p;
int next;
} que[maxn<<]; void addedge(int a,int b,double c)
{
que[cnt].v=b;
que[cnt].p=c;
que[cnt].next=first[a];
first[a]=cnt;
cnt++;
}
void spfa(int u)
{
memset(tp,,sizeof(tp));
tp[u]=;
memset(vis,false,sizeof(vis));
vis[u]=true;
memset(before,-,sizeof(before));
for(int i=;i<=n;i++)
dis[i]=inf;
dis[u]=; queue<int>q;
q.push(u);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false; for(int i=first[x]; i!=-; i=que[i].next)
{
int v=que[i].v;
if(dis[v]>dis[x]+)
{
dis[v]=dis[x]+;
before[v]=x;
tp[v]=tp[x]*que[i].p;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
else if(dis[v]==dis[x]+)
{
if(tp[x]*que[i].p>tp[v])
{
tp[v]=tp[x]*que[i].p;
before[v]=x; if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
} }
return ;
}
int ans[maxn];
void outp(int tend){
memset(ans,,sizeof(ans));
int cnt=-;
for(int i=tend;i!=-;i=before[i]){
ans[++cnt]=i;
} for(int i=cnt;i>=;i--){
printf("%d%c",ans[i],i==?'\n':' ');
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{ memset(first,-,sizeof(first));
int start,tend;
scanf("%d%d",&start,&tend);
cnt=;
for(int i=; i<=m; i++)
{
int t1,t2;
double t3;
scanf("%d%d%lf",&t1,&t2,&t3);
addedge(t1,t2,-t3/);
addedge(t2,t1,-t3/);
} spfa(start);
printf("%d %.8lf\n",dis[tend]+,-tp[tend]);
outp(tend); }
return ;
}
URAL 1934 spfa算法的更多相关文章
- 最短路径问题的Dijkstra和SPFA算法总结
Dijkstra算法: 解决带非负权重图的单元最短路径问题.时间复杂度为O(V*V+E) 算法精髓:维持一组节点集合S,从源节点到该集合中的点的最短路径已被找到,算法重复从剩余的节点集V-S中选择最短 ...
- [知识点]SPFA算法
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vx93.html 1.前言 ...
- SPFA算法
SPFA算法 一.算法简介 SPFA(Shortest Path Faster Algorithm)算法是求单源最短路径的一种算法,它是Bellman-ford的队列优化,它是一种十分高效的最短路算法 ...
- SPFA算法学习笔记
一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- SPFA算法心得
SPFA算法是改进后的Bellman-Ford算法,只是速度更快,而且作为一个算法,它更容易理解和编写,甚至比Dijkstra和B-F更易读(当然,Floyd是另一回事了,再也没有比Floyd还好写的 ...
- 最短路径--SPFA 算法
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...
- Bellman-Ford & SPFA 算法——求解单源点最短路径问题
Bellman-Ford算法与另一个非常著名的Dijkstra算法一样,用于求解单源点最短路径问题.Bellman-ford算法除了可求解边权均非负的问题外,还可以解决存在负权边的问题(意义是什么,好 ...
- UVA 10000 Longest Paths (SPFA算法,模板题)
题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...
随机推荐
- jQuery实现轮播切换以及将其封装成插件(3)
在前两篇博文中,我们写了一个普通的轮播切换.但是我们不能每一次需要这个功能就把这些代码有重新敲一次.下面我们就将它封装成一个插件. 至于什么是插件,又为什么要封装插件,不是本文考虑的内容. 我们趁着 ...
- JS学习笔记--变量类型
1.js数据类型分为基本数据类型和引用数据类型 基本数据类型:string.number.boolean.null.undefined.symbol(ES6中新增) 引用数据类型:object.arr ...
- MySQL-常用的存储引擎
MySQL-常用的存储引擎 存储引擎 事务 锁粒度 主要应用 忌用 MyISAM 不支持 支持并发插入的表级锁 select,insert 读写操作频繁 MRG_MYISAM 不支持 支持并发插入的表 ...
- AsyncDisplayKit技术分析
转载请注明出处:http://xujim.github.io/ios/2014/12/07/AsyncDisplayKit_inside.html ,谢谢 前言 Facebook前段时间发布了其iOS ...
- 使用winsw将spring-boot jar包注册成windows服务
背景:最近的项目中使用spring-boot, https://github.com/kohsuke/winsw/releases <service> <id>YJPSS< ...
- Django 入门案例开发
Django是一个重量级的web开发框架,它提供了很多内部已开发好的插件供我们使用:这里不去描述 Django直接进入开发过程. Django入门案例分两部分:一.开发环境的配置:二.业务需求分析. ...
- 交换机基础配置之跨交换机划分vlan
我们以上面的拓扑图来进行实验 四台pc机都在同一网段 pc1和pc2在同一台交换机上 pc3和pc4在同一台交换机上 现在我们实验的目的就是将pc1和pc3划分到同一vlan pc2和pc4划分到同一 ...
- L2-032 彩虹瓶 (25 分)
L2-032 彩虹瓶 (25 分) 彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小 ...
- No module named appium
在脚本中会有:from appium import webdriver 第一次运行时可能会遇到这样的error:No module named appium 之所以会报这样的error是因为没有装cl ...
- Eclipse EE 运行 Myeclipse Web 应用
第一步:进入项目所在目录,打开.project文件,找到<natures>...</natures>标签,在中间添加如下子标签内容 <nature>org.ecli ...