bzoj5197:[CERC2017]Gambling Guide

Portal

Also I wrote some seem to expect the probability of problems, but futile, routine or not, read the explanations will write

First set \ (f [x] \) of \ (X \) to the \ (n-\) a desired minimum number of steps, \ (deg_x \) represents \ (X \) degrees

Does not consider fixed, apparently \ (f [x] = \ sum _ {(x, y) \ in E} \ frac {f [y] +1} {deg_x} \)

Since it is not fixed, \ (F [X] = \ _ {SUM (X, Y) \ in E} \ {FRAC \ min (F [X], F [Y])} + {deg_x. 1} \)

Then we actually is, only when (f [y] <f [ x] \) \ time will produce a contribution

The conditions and \ (\ rm dijkstra \) relaxation operation is very similar, we can use the \ (\ rm dijkstra \) is calculated

But this formula is not good to maintain because only \ (f [y] <f [x] \) time will generate contributions, then a lot of the enumeration are invalid

So we can change it formula \ (f [x] = \ frac {deg_x + \ sum _ {(x, y) \ in E, f [y] <f [x]} f [y]} {\ sum _ {( x, y) \ in E} [f [y] <f [x]]} \)

This can be dynamically maintained

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
void read(int &x){
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3e5+10;bool vis[maxn];
int n,m,cnt,pre[maxn*2],nxt[maxn*2],h[maxn];
double f[maxn],sum[maxn],in[maxn],t[maxn];
struct oo{double x;int y;};
priority_queue<oo>q;
bool operator<(oo a,oo b){return a.x>b.x;}
void add(int x,int y){
    pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,in[x]++;
    pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,in[y]++;
}
void dijkstra(){
    memset(f,127,sizeof f);
    f[n]=0,q.push((oo){f[n],n});
    while(!q.empty()){
        int x=q.top().y;q.pop();
        if(vis[x])continue;vis[x]=1;
        for(rg int i=h[x];i;i=nxt[i])
            if(f[pre[i]]>=f[x]){
                t[pre[i]]++,sum[pre[i]]+=f[x];
                f[pre[i]]=(in[pre[i]]+sum[pre[i]])/t[pre[i]];
                q.push((oo){f[pre[i]],pre[i]});
            }
    }
}
int main(){
    read(n),read(m);
    for(rg int i=1,x,y;i<=m;i++)read(x),read(y),add(x,y);
    dijkstra(),printf("%.6lf",f[1]);
}

Guess you like

Origin www.cnblogs.com/lcxer/p/11099223.html