zoukankan      html  css  js  c++  java
  • bzoj 3036: 绿豆蛙的归宿

    Description

    随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿。

    给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度。绿豆蛙从起点出发,走向终点。
    到达每一个顶点时,如果有K条离开该点的道路,绿豆蛙可以选择任意一条道路离开该点,并且走向每条路的概率为 1/K 。
    现在绿豆蛙想知道,从起点走到终点的所经过的路径总长度期望是多少?

    Input

    第一行: 两个整数 N M,代表图中有N个点、M条边
    第二行到第 1+M 行: 每行3个整数 a b c,代表从a到b有一条长度为c的有向边

    Output


    从起点到终点路径总长度的期望值,四舍五入保留两位小数。

    Sample Input

    4 4
    1 2 1
    1 3 2
    2 3 3
    3 4 4

    Sample Output

    7.00

    HINT

    对于100%的数据  N<=100000,M<=2*N

    ————————————————————————

    这道题就是裸的期望了吧QAQ

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    const int M=2e5+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    double ans;
    int n,m;
    int first[M],cnt,out[M];
    struct node{int to,next,w;}e[2*M];
    void ins(int a,int b,int w){e[++cnt]=(node){b,first[a],w}; first[a]=cnt;}
    void dfs(int x,int sum,double ly){
        if(x==n){ans=ans+sum*ly;return ;}
        for(int i=first[x];i;i=e[i].next){
            int now=e[i].to;
            dfs(now,sum+e[i].w,ly*1.0/(1.0*out[x]));
        }
    }
    int main(){
        int x,y,w;
        n=read(); m=read();
        for(int i=1;i<=m;i++){
            x=read(); y=read(); w=read();
            ins(x,y,w); out[x]++;
        }
        dfs(1,0,1.0); printf("%.2f
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    手机操作
    模拟手机操作
    get_attribute_value
    test_order
    信息收集-FOFA资产收集与FOFA api
    html中form讲解
    安装redis
    yum vs rpm
    yum 安装java环境
    显示当前目录所有文件大小的命令ls -lht
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7683970.html
Copyright © 2011-2022 走看看