The Preliminary Contest for ICPC Asia Nanjing 2019 D. Robots(概率dp)

Topic links: https://nanti.jisuanke.com/t/41301

Subject to the effect:

Given a cycle have no, it starts from the node to FIG. 1, the end node n.

There is a robot from the beginning, every day, with equal probability one of the nodes or visit the adjacent stationary. Days durability daily consumption is equal robot passes.

Calculate the robot to reach the expected durability consumption at node n.

Ensure that only one node (node ​​1) of the in-degree equal to 00, and only one out-degree node (node ​​n) is not equal to 0. plurality of edges and FIG.

 

Problem-solving ideas:

Dp [i] is provided from i n to reach the end of the desired time it is easy to obtain:

 

Ans [i] is provided next to the duration value from the consumption amount reaches the end of i, then the state transition equation is ans ans = [u] [u] * 1 (outdeg [u] +1) + 1 / (outdey [u] +1) * Σans [v] [u] + dp. As this picture shows the directed acyclic graph, it can be run side transfer topological edges.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
struct st{
    int to,next;
}stm[maxn*2];
int cnt;
int head[maxn];
void add(int u,int v){
    stm[cnt].to=v;
    stm[cnt].next=head[u];
    head[u]=cnt++;
}
int cd[maxn];
int cd1[maxn];
double dp[maxn];
double ans[maxn];
queue<int> que;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        int u,v;
        memset(head,-1,sizeof(head));
        memset(cd,0,sizeof(cd));
        memset(dp,0,sizeof(dp));
        memset(ans,0,sizeof(ans));
        memset(cd1,0,sizeof(cd1));
        cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            add(v,u);
            cd[u]++;
            cd1[u]++;
        }
        while(!que.empty())que.pop();
        que.push(n);
        while(!que.empty()){
            int now=que.front();
            que.pop();
            for(int i=head[now];~i;i=stm[i].next){
                int to=stm[i].to;
                dp[to]+=1.0/(cd[to]+1)*dp[now];
                ans[to]+=1.0/(cd[to]+1)*ans[now];
            //1.0*(cd[to]+1)/cd[to];
                cd1[to]--;
                if(cd1[to]==0){
                    dp[to]=(dp[to]+1)*1.0*(cd[to]+1)/cd[to];
                    ans[to]=(ans[to]+dp[to])*1.0*(cd[to]+1)/cd[to];
                    que.push(to);
                }
            }
        }    
        printf("%.2lf\n",ans[1]);
    }
    return 0;
}

 

  

 

Guess you like

Origin www.cnblogs.com/Zhi-71/p/11444076.html