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 < ...
随机推荐
- C#把日期转化成星期
显示效果: ***** 前台页面代码: <TextBlock Grid.Row="/> <TextBlock Grid.Row="/> ...
- 【图论 思维】cf715B. Complete The Graph加强
zzq讲的杂题 题目大意 有一张$n$个点$m$条边的简单正权无向图,$S$到$T$的最短路为$L$,现在有一些边的边权未知,请输出任意一种满足题意的方案. $n,m\le 500000$ ...
- Struts2基础学习2
Struts2基础学习2 项目结构,测试页面与实体类 <%@ page language="java" contentType="text/html; charse ...
- Cloudera Manager 安装 CDH5
文档说明 本文是针对Linux CentOS6服务器与CDH5.15的安装手册. 关于CDH和ClouderaManager CDH(Cloudera's Distribution, includin ...
- tcl之文件操作-文件名相关
- 超简单开发自己的php框架一点都不难
(转)https://blog.csdn.net/qq_33862644/article/details/79344331 写框架的极简思路: 接收,打印参数想怎么弄.如 获取配置文件的方法,根据传过 ...
- POJ:3258-River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17740 Accepted: 7414 Desc ...
- [BZOJ3172 ][Tjoi2013]单词(AC自动机)
Description 不稳定的传送门 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次.单词个数<=200,单词总长度< ...
- Snowflake Snow Snowflakes【Poj3349】
Description You may have heard that no two snowflakes are alike. Your task is to write a program to ...
- Java Spring Controller 获取请求参数的几种方法
技术交流群:233513714 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...