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. 统计函数运行时间-CPU端

    C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下:  clock_t clock( void ); 这个函数返回从“开启这个程序 ...

  2. iPad游戏 Calcculator: The Game 程序自动计算求解方法

    今天在iPad上下了个小游戏,主要是一个计算器的界面,有开始值,目标值,限定步数,以及一些加减乘除,还有作者脑洞想出来的功能键,主要有左移,直接把一个数加到末尾,将其中的某个数改为另一个数等等..玩到 ...

  3. es6-promise.auto.js

    使用sweetalert2的IE浏览器报错,导入文件 链接:https://pan.baidu.com/s/1mOcsN_o8m-7I7Rej1NPkiw 提取码:9xsj

  4. 修改第三方库内容,carsh提示"image not found"

    在图示位置把提示的东西加上即可 参考: iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta ...

  5. jquery动态改变元素内容

    ● text() - 设置或返回所选元素的文本内容 ● html() - 设置或返回所选元素的内容(包括 HTML 标记) ● val() - 设置或返回表单字段的值(只针对表单或者输入框)

  6. java @override 全部报错

    问.java @override 全部报错 答: 错误:在 eclipse 的新工作空间开发项目时,出现大面积方法编译错误.鼠标放在方法名上后显示让我们去掉 @override 注解 原因: @Ove ...

  7. php-语言参考-基本语法3.1

    一,PHP代码的开始和结束标记 1,<?php 和 ?> //重点 2,<script language="php"> 和 </script> ...

  8. APUE中对出错函数的封装

    // 输出至标准出错文件的出错处理函数static void err_doit(int, int, const char *, va_list); /* * Nonfatal error relate ...

  9. ELK之Elasticsearch

    安装并运行Elasetisearch cd elasticsearch-<version> ./bin/elasticsearch 如果你想把 Elasticsearch 作为一个守护进程 ...

  10. [Bzoj2246]迷宫探险(概率+DP)

    Description 题目链接 Solution 用三进制表示陷阱状态,1表示有害,2表示无害,0表示不知道 用\(f[S][i]\)表示状态为S时陷阱i有害的概率,这个可以预处理出 \(d[S][ ...