zoukankan      html  css  js  c++  java
  • BZOJ 3036: 绿豆蛙的归宿( 期望dp )

     从终点往起点倒推 .

    在一个图 考虑点 u , 出度为 s : s = 0 , d[ u ] = 0 ; s ≠ 0 , 则 d( u ) = ( ∑ d( v ) ) / s ( ( u , v ) ∈ E ) 

    ----------------------------------------------------------------------------

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<vector>
     
    #define clr( x , c ) memset( x , c , sizeof( x ) )
    #define rep( i , n ) for( int i = 0 ; i < n ; ++i )
    #define mp make_pair
     
    using namespace std;
     
    typedef pair< int , int > pii;
     
    const int maxn = 1e5 + 5;
     
    vector< pii > g[ maxn ]; // { to , dist }
    int n;
    double d[ maxn ];
    bool vis[ maxn ];
     
    void dfs( int x ) {
    vis[ x ] = true;
    int s = g[ x ].size();
    rep( i , s ) {
    if( ! vis[ g[ x ][ i ].first ] ) dfs( g[ x ][ i ].first );
    d[ x ] += d[ g[ x ][ i ].first ] + g[ x ][ i ].second;
    }
    if( s ) d[ x ] /= s;
    }
     
    int main() {
    // freopen( "test.in" , "r" , stdin );
    int m;
    cin >> n >> m;
    while( m-- ) {
    int u , v , d;
    scanf( "%d%d%d" , &u , &v , &d );
    --u , --v;
    g[ u ].push_back( mp( v , d ) );
    }
    clr( d , 0 ) , clr( vis , 0 );
    dfs( 0 );
    printf( "%.2lf " , d[ 0 ] );
        return 0; 

      

    ----------------------------------------------------------------------------

    3036: 绿豆蛙的归宿

    Time Limit: 2 Sec  Memory Limit: 128 MB
    Submit: 211  Solved: 149
    [Submit][Status][Discuss]

    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

    Source

  • 相关阅读:
    Aizu
    Aizu
    POJ
    POJ
    POJ
    UVA
    manacher
    Gym
    Gym
    Gym
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4643073.html
Copyright © 2011-2022 走看看