码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 3036 绿豆蛙的归宿 期望dp

时间:2018-08-14 19:52:54      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:.com   cpp   href   sum   struct   return   pac   math   时间复杂度   

题面

题目传送门

解法

$\(f_x=\sum \frac{f_y+w(x,y)}{out_x}\)

因为是一个DAG,直接记忆化即可

时间复杂度:\(O(n+m)\)

代码

#include <bits/stdc++.h>
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
struct Edge {
    int next, num, v;
} e[N * 5];
int n, m, cnt, s[N];
double f[N];
void add(int x, int y, int v) {
    e[++cnt] = (Edge) {e[x].next, y, v};
    e[x].next = cnt;
}
double dp(int x) {
    if (x == n) return 0;
    if (f[x]) return f[x];
    for (int p = e[x].next; p; p = e[p].next) {
        int k = e[p].num, v = e[p].v;
        f[x] += (dp(k) + v) / s[x];
    }
    return f[x];
}
int main() {
    read(n), read(m); cnt = n;
    for (int i = 1; i <= m; i++) {
        int x, y, v;
        read(x), read(y), read(v);
        add(x, y, v); s[x]++;
    }
    cout << fixed << setprecision(2) << dp(1) << "\n";
    return 0;
}

bzoj 3036 绿豆蛙的归宿 期望dp

标签:.com   cpp   href   sum   struct   return   pac   math   时间复杂度   

原文地址:https://www.cnblogs.com/copperoxide/p/9476737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!