D - Black Spot

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Bootstrap: Jones's terrible leviathan will find you and drag the Pearl back to the depths and you along with it.
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.
Captain Jack Sparrow has got a black spot on his hand and he avoids
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.
There are n
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.
Jack is in a hurry and he wants to reach Tortuga visiting as small
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

The first line contains two integers n,
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

In the first line output k — number of islands along the path and p — probability to meet Kraken on that path. An absolute error of p should be up to 10 −6. In the next line output k integers — numbers of islands in the order of the path. If there are several solutions, output any of them.

Sample Input

input output
4 4
1 3
1 2 50
2 3 50
1 4 10
4 3 10
3 0.19
1 4 3
#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算法的更多相关文章

  1. 最短路径问题的Dijkstra和SPFA算法总结

    Dijkstra算法: 解决带非负权重图的单元最短路径问题.时间复杂度为O(V*V+E) 算法精髓:维持一组节点集合S,从源节点到该集合中的点的最短路径已被找到,算法重复从剩余的节点集V-S中选择最短 ...

  2. [知识点]SPFA算法

    // 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vx93.html 1.前言 ...

  3. SPFA算法

    SPFA算法 一.算法简介 SPFA(Shortest Path Faster Algorithm)算法是求单源最短路径的一种算法,它是Bellman-ford的队列优化,它是一种十分高效的最短路算法 ...

  4. SPFA算法学习笔记

    一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...

  5. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  6. SPFA算法心得

    SPFA算法是改进后的Bellman-Ford算法,只是速度更快,而且作为一个算法,它更容易理解和编写,甚至比Dijkstra和B-F更易读(当然,Floyd是另一回事了,再也没有比Floyd还好写的 ...

  7. 最短路径--SPFA 算法

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  8. Bellman-Ford & SPFA 算法——求解单源点最短路径问题

    Bellman-Ford算法与另一个非常著名的Dijkstra算法一样,用于求解单源点最短路径问题.Bellman-ford算法除了可求解边权均非负的问题外,还可以解决存在负权边的问题(意义是什么,好 ...

  9. UVA 10000 Longest Paths (SPFA算法,模板题)

    题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...

随机推荐

  1. FTP服务安装及使用

    准备工作:一台服务器.我这里使用的是阿里云的ECS. 环境使用的是:windows 2008 r2 用途:FTP是用来进行文件传输的,我们可以把这个目录在IIS上配置成发布的网站,我们在本地只用把我们 ...

  2. convolution,fft, 加速

    零零星星挖坑几个了,都没填土,实在是欠账太多,闲话少说吧,还是多记录总结一下.今天的主题是围绕convolution和加速 记得之前看过lecun他们组的一篇文章,是fft加速convolution的 ...

  3. python面试,日更

    l1 = [11, 2, 3, 22, 2, 4, 11, 3] 去重并保持原来顺序 # 集合方法 l2 = list(set(l1)) l2.sort(key=l1.index) # 按照l1索引排 ...

  4. SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件

    简介 SVProgressHUD是简单易用的显示器,用于指示一个持续进行的任务的进度. 项目主页: SVProgressHUD 最新示例: 点击下载 快速入门 安装 通过Cocoapods pod ' ...

  5. tomcat7下载地址

    tomcat7下载地址:https://tomcat.apache.org/download-70.cgi

  6. 想学习一下node.js,重新安装配置了node

    根据这个网站上的教程安装配置的,还不错一次就成功了.觉得安装没什么,就是配置路径的时候容易错. http://www.runoob.com/nodejs/nodejs-install-setup.ht ...

  7. linux防火墙的管理和策略控制

    iptables 一:IPtables防火墙的简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 ...

  8. php扩展开发-资源类型

    资源类型在内核中的结构 //zend_list.h typedef struct _zend_rsrc_list_entry { void *ptr; int type; int refcount; ...

  9. 插入排序算法Java实现

    一. 算法描述 插入即表示将一个新的数据插入到一个有序数组中,并继续保持有序.例如有一个长度为N的无序数组,进行N-1次的插入即能完成排序:第一次,数组第1个数认为是有序的数组,将数组第二个元素插入仅 ...

  10. mysql学习第二天函数

    -- 1.绝对值 select abs(-1)from dual -- 2.求平方根select sqrt(6)from dual -- 3.圆周率select pi()from dual -- 4. ...