URAL 1934 spfa算法

杰克船长需要找到一条从当前岛屿到托尔图加的路径,这条路径不仅要经过最少数量的岛屿,还要尽可能降低遇到海怪的风险。本篇介绍了一种算法解决此问题,通过概率计算帮助杰克船长避开危险。
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, tn; st). 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 in; a ib 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

inputoutput
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=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<int>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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值