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. c/c++基础

    如果有你认为重要的知识点,而我这却没有记录下来的,那么期待你分享给我(^U^)ノ~YO. 1.在结构体中,符号->的前面是指针变量,符号.的前面是普通变量.   程序中a->b等价于(*a ...

  2. 《JavaScript高级程序设计第三版》——细碎知识痛点整理(第六章)

    面向对象的程序设计 对象是一组没有特定顺序的值6.1.1 属性类型ECMAScript中有两种属性:数据属性和访问器属性.1. 数据属性Configurable 表示能否通过delete删除属性从而重 ...

  3. iOS第三方支付(支付宝)

    使用支付宝进行一个完整的支付功能,大致有以下步骤: 与支付宝签约,获得商户ID(partner)和账号ID(seller) 下载相应的公钥私钥文件(加密签名用) 下载支付宝SDK 生成订单信息 调用支 ...

  4. 常见的HTTP状态码有哪些?

    当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求. ...

  5. Centos7下MySql5.7安装及配置

    安装MySql 软件包: mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-community-common-5.7.22-1.el7.x86_64 ...

  6. spring开篇

    本文引用http://www.cnblogs.com/ityouknow/p/5292559.html spring简介: spring是一个开源框架,spring是于2003 年兴起的一个轻量级的J ...

  7. python之微信自动发送消息

    代码如下: from __future__ import unicode_literals from threading import Timer from wxpy import * import ...

  8. 【异常】The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.

    异常错误:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone ...

  9. 笔记-git-协作开发

    笔记-git-协作开发 1.      git协作开发 git协作的典型做法是,创建一个git服务器,被多个人操作. 示意图如下: 一般来说协作分为如下几个步骤: 创建一个git裸服务器 (git i ...

  10. 搭建Linpack

    环境:vmware workstation14 + centos7(linux基本都可以) 一.开始安装mpich 1. 解决依赖gcc gcc-gfortran sudo yum install g ...