概率与期望

绿豆蛙的期望

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=100000+10;

struct my{
    int next;
    int v;
    double w;
};

bool vis[maxn];
int dp[maxn];
int tot,adj[maxn];
int n,m;
double totans;
int in[maxn];
my edge[maxn*2];

void myinsert(int u,int v,int w){
     edge[++tot].next=adj[u];
     edge[tot].v=v;
     edge[tot].w=w;
     adj[u]=tot;
     in[u]++;
}

double dfs(int x){
       if(x==n) return 0;
       //vis[x]=true;
       double ans=0;
       for (int i=adj[x];i;i=edge[i].next){
            int v=edge[i].v;
         //   if(vis[v]) continue;
            ans+=(dfs(v)+edge[i].w)/in[x];
       }
       //totans+=ans;
       return ans;
}

int main(){
    int u,v,w;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        scanf("%d%d%d",&u,&v,&w);
        myinsert(u,v,w);
    }
    //dfs(1);
    printf("%.2lf\n",dfs(1));
return 0;
}

 

posted @ 2018-10-16 22:25  lmjer  阅读(98)  评论(0编辑  收藏  举报