URAL 1934 Black Spot --- 最短的简单修改

右侧是1。维护的同时保持最短路p值至少,我有直接存款(1-p)。该概率不满足,为了使这个值极大。


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll long long
const int maxn=100010;
const int maxm=100010;
using namespace std;

struct node
{
    int v,w,next;
    double p;
}e[maxm<<1];
int vis[maxn],h,head[maxn],n,m,d[maxn],pre[maxn];
double p[maxn];

void addedge(int a,int b,double c)
{
    e[h].v=b;
    e[h].w=1;
    e[h].p=c;
    e[h].next=head[a];
    head[a]=h++;
}

void spfa(int s)
{
    int x,v,i;
    for(i=0;i<=n;i++)
        p[i]=0,d[i]=inf;
    memset(vis,0,sizeof vis);
    memset(pre,-1,sizeof pre);
    p[s]=1,vis[s]=1,d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        vis[x]=0;
        for(i=head[x];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(d[v]>d[x]+1)
            {
                d[v]=d[x]+1;
                p[v]=p[x]*e[i].p;
                pre[v]=x;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
            else if(d[v]==d[x]+1)
            {
                if(p[v]<p[x]*e[i].p)
                {
                    p[v]=p[x]*e[i].p;
                    pre[v]=x;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    return ;
}
int flag;
void output(int x)
{
    if(pre[x]!=-1)
        output(pre[x]);
    if(flag) flag=0;
    else putchar(' ');
    printf("%d",x);
}

int main()
{
    int a,b,s,t;
    double c;
    while(~scanf("%d%d",&n,&m))
    {
        h=0;
        memset(head,-1,sizeof head);
        scanf("%d%d",&s,&t);
        while(m--)
        {
            scanf("%d%d%lf",&a,&b,&c);
            addedge(a,b,1-c/100);
            addedge(b,a,1-c/100);
        }
        spfa(s);
        printf("%d %.8lf
",d[t]+1,1-p[t]);
        flag=1;
        output(t);
        puts("");
    }
    return 0;
}


版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4912371.html