Probability dp section title

Record some of the more water is not worth writing a separate blog probability dp topic


bzoj3036 frog resting place

Description

With the new version of Baidu offline space, Blog pet frog to complete its mission, to find it a new home.

Given a directed acyclic connected graph, a starting point for the end point is N, each edge has a length. Frog from the starting point, towards the end.
Each vertex is reached, if the road away from the K point, a frog can select any point away from the road, and the probability to each path is \ (\ FRAC. 1 {{}} K \) .
Now frog would like to know, from the beginning to an end of a path through which the total length of expectations is how much?

Input

The first line: two integers NM, there are represented in Figure N points, M edges
of the second row to row 1 + M: an integer of 3 per row abc, representatives from a to b to c have a length directional side

Output

End of the path from the starting point to a desired value of the total length, rounded to two decimal places.

Hint

To 100% of the data, \ (N \ 100000 Leq, M \ Leq 2N \)

That bzoj and write questions and they in turn has blown up, this place is a dbzoj link
saying this morning to do [SCOI2008] bonus levels that title, along push against push for a long time could not understand, to be a water problem relax

Direct reset state \ (F_i \) from \ (I \) to the \ (n-\) of the desired path length
dfs directly to the length of time of each path together and then dividing it by a degree of the line

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
    int x=0,y=1;
    char c=std::getchar();
    while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
    while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
    return y?x:-x;
}
int n,m;
double f[100006];
int vis[100006],out[100006];
int fir[100006],nex[200005],to[200006],w[200006];
void dfs(int x){
    if(vis[x]) return;
    vis[x]=1;
    for(reg int v,i=fir[x];i;i=nex[i]){
        v=to[i];
        dfs(v);
        f[x]+=f[v]+w[i];
    }
    if(out[x]) f[x]/=out[x];
}
int main(){
    n=read();m=read();
    for(reg int x,y,z,i=1;i<=m;i++){
        x=read();y=read();z=read();
        to[i]=y;w[i]=z;
        nex[i]=fir[x];fir[x]=i;
        out[x]++;
    }
    dfs(1);
    std::printf("%.2lf",f[1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/suxxsfe/p/12523850.html