The Preliminary Contest for ICPC Asia Nanjing 2019/2019 season Nanjing network - solution to a problem

(Construction ......)

Game Portal: https://www.jisuanke.com/contest/3004

D. Robots (expected dp)

The meaning of problems

The DAG to one, to ensure the degree of a point only $ 0 $ 1 $ $, $ a $ 0 degree point only $ n $.

Now a robot from $ 1 $, every day, with equal probability one of the nodes or visit the adjacent stationary.

Daily consumption of durable robot is equal to the number of days elapsed.

Seeking robot point $ n $ durable consumption expectations.

The water draw very happy, the only one made out of the question. But the solution to a problem and practice different (feeling my way of trouble), and therefore smashed 3h at this question above (solution to a problem is trying to understand ing).

Provided $ f [u] [j] $ denotes $ J $ days from the viewpoint of $ U $ to durable $ n-$ desired consumption, $ out [i] $ denotes the degree $ I $ a $ + 1 $, then The answer is $ f [1] [1] $.

The initial equations do not write very easily.

After a large meal derived can be determined $ f [u] [j] = \ frac {out [u]} {out [u] -1} \ times j + \ frac {out [u]} {(out [u] -1) ^ 2} + \ sum_v (\ frac {f [v] [j + 1]} {out [u]} + \ frac {f [v] [j + 2]} {out [u] ^ 2 + ...}) $, $ V wherein U $ $ $ to neighboring nodes.

Behind the wonderful things that point is difficult to deal with, we might first think for $ 1 -> 2 $ such a map, $ f [1] [j] $ How much?

Hey why this thing is arithmetic sequence?

So we assume $ f [v] [j] $ is an arithmetic sequence, then the original equation can be reduced to $ f [u] [j] = \ frac {out [u]} {out [u] -1} \ times j + \ frac {out [u]} {(out [u] -1) ^ 2} + \ sum_v \ frac {f [v] [j + 1] * out [u] -f [v] [j ]} {(out [u] -1) ^ 2} $, in short, you can obtain $ f [u] [j] $ is also wants arithmetic sequence.

Thus mathematical induction can be determined all $ f [u] [j] $ are arithmetic sequence, we only required $ J $ $ $ 1 and $ 2 $, can then seek, $ complexity from the back O (n + m) $, specific implementation details and look at the code.

#include<cmath>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double dl;
const int N=1e5+5;
const int M=2e5+5;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
struct node{
    int to,nxt;
}e[M];
int n,m,cnt,head[N],out[N],dep[N];
dl f[N][3];
inline void add(int u,int v){
    e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;out[u]++;
}
void init(){
    for(int i=1;i<=n;i++){
        head[i]=0;out[i]=1;
        f[i][1]=f[i][2]=0;
    }
    cnt=0;
}
dl F(int u,int j){
    if(u==n)return 0;
    if(f[u][1]>0&&f[u][2]>0)return (f[u][2]-f[u][1])*(j-1)+f[u][1];
    dl sum=(dl)out[u]/(out[u]-1)*j+(dl)out[u]/(out[u]-1)/(out[u]-1);
    for(int i=head[u];i;i=e[i].nxt){
        int v=e[i].to;
        dl div=(out[u]-1)*(out[u]-1);
        dl a1=-F(v,j);dl a2=F(v,j+1)*out[u];
        sum+=(a1+a2)/div;
    }
    return f[u][j]=sum;
}
int main(){
    int T=read();
    for(int cas=1;cas<=T;cas++){
        n=read(),m=read();
        init();
        for(int i=1;i<=m;i++){
            int u=read(),v=read();add(u,v);
        }
        printf("%.2Lf\n",F(1,1));
    }
    return 0;
}
View Code

 

+++++++++++++++++++++++++++++++++++++++++++

 + Author: luyouqi233. +

 + Welcome to my blog: http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin www.cnblogs.com/luyouqi233/p/11444023.html