博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
URAL 1934 spfa算法
阅读量:4879 次
发布时间:2019-06-11

本文共 4013 字,大约阅读时间需要 13 分钟。

D - Black Spot
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit

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
a
i and
b
i where the route is known and
p
i — probability to meet Kraken on that route as percentage (1 ≤
a
i,
b
i
n;
a
i
b
i; 0 ≤
p
i ≤ 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 41 31 2 502 3 501 4 104 3 10
3 0.191 4 3

 

 

 

#include
#include
#include
#include
#include
using namespace std;const int maxn=110000;int n,m;bool vis[maxn];int before[maxn],dis[maxn];const int inf=99999999;int first[maxn];int cnt;double tp[maxn];struct node{ int v; double p; int next;} que[maxn<<1];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,0,sizeof(tp)); tp[u]=1; memset(vis,false,sizeof(vis)); vis[u]=true; memset(before,-1,sizeof(before)); for(int i=0;i<=n;i++) dis[i]=inf; dis[u]=0; queue
q; q.push(u); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=false; for(int i=first[x]; i!=-1; i=que[i].next) { int v=que[i].v; if(dis[v]>dis[x]+1) { dis[v]=dis[x]+1; 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]+1) { 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,0,sizeof(ans)); int cnt=-1; for(int i=tend;i!=-1;i=before[i]){ ans[++cnt]=i; } for(int i=cnt;i>=0;i--){ printf("%d%c",ans[i],i==0?'\n':' '); }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(first,-1,sizeof(first)); int start,tend; scanf("%d%d",&start,&tend); cnt=0; for(int i=1; i<=m; i++) { int t1,t2; double t3; scanf("%d%d%lf",&t1,&t2,&t3); addedge(t1,t2,1-t3/100); addedge(t2,t1,1-t3/100); } spfa(start); printf("%d %.8lf\n",dis[tend]+1,1-tp[tend]); outp(tend); } return 0;}

 

转载于:https://www.cnblogs.com/13224ACMer/p/4685266.html

你可能感兴趣的文章
全网最详细的Windows系统里PLSQL Developer 64bit的下载与安装过程(图文详解)
查看>>
自动化测试用例getText()获取某一个元素的值返回null或空
查看>>
大数智能未来
查看>>
virtualenv和virtualenvwrapper 的安装和使用
查看>>
MAC sublime text 无法自动补齐标签
查看>>
NgBook留言本开发全过程(1)
查看>>
LeetCode-指针法
查看>>
Python之路,Day12 - 那就做个堡垒机吧
查看>>
linux之shell之if、while、for语句介绍
查看>>
Mysql phpStudy升级Mysql版本,流产了怎么办?
查看>>
SQLServer之数据库行锁
查看>>
OFDM仿真
查看>>
浅谈linux内核中内存分配函数
查看>>
走近SpringBoot
查看>>
thinkphp3.2.3分页
查看>>
python程序之profile分析
查看>>
写在读研初期
查看>>
开环增益对负反馈放大电路的影响
查看>>
MySQL-ERROR 2003
查看>>
SQL Server2012-SSIS的包管理和部署
查看>>