bzoj 3036 绿豆蛙的归宿 期望dp

题面

题目传送门

解法

\[f_x=\sum \frac{f_y+w(x,y)}{out_x}$ 因为是一个DAG,直接记忆化即可 时间复杂度:$O(n+m)$ ### 代码 ```cpp #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; } ```\]

posted @ 2018-08-14 18:55  谜のNOIP  阅读(71)  评论(0编辑  收藏  举报