AC日记——绿豆蛙的归宿 codevs 2488

绿豆蛙的归宿

 

 

思路:

  topsort+期望dp;

 

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 100005

int head[maxn],cnt,E[maxn<<2],V[maxn<<2],du[maxn];
int sta[maxn],top,n,m;

double W[maxn<<2],dp[maxn][2],num[maxn];

inline void in(int &now)
{
    char Cget=getchar();now=0;
    while(Cget>'9'||Cget<'0') Cget=getchar();
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
}

inline void edge_add(int u,int v,double w)
{
    E[++cnt]=head[u],V[cnt]=v,W[cnt]=w,head[u]=cnt;
}

int main()
{
    in(n),in(m);int u,v;double w;
    for(;m--;)
    {
        in(u),in(v),du[v]++;
        num[u]+=1.0;
        scanf("%lf",&w);
        edge_add(u,v,w);
    }
    dp[1][1]=1,sta[++top]=1;
    while(top>0)
    {
        int now=sta[top--];
        for(int i=head[now];i;i=E[i])
        {
            dp[V[i]][1]+=dp[now][1]/num[now],du[V[i]]--;
            dp[V[i]][0]+=(dp[now][1]*W[i]+dp[now][0])/num[now];
            if(!du[V[i]]) sta[++top]=V[i];
        }
    }
    printf("%.2lf",dp[n][0]);
    return 0;
}

 

posted @ 2017-05-23 21:53  IIIIIIIIIU  阅读(226)  评论(0编辑  收藏  举报