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 ai and bi where the route is known and pi — probability to meet Kraken on that route as percentage (1 ≤ ai, bin; aibi; 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

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;
}

 

posted @ 2015-07-29 10:44  柳下_MBX  阅读(246)  评论(0编辑  收藏  举报