bzoj 3036 綠豆蛙的歸宿 期望dp
阿新 • • 發佈:2018-08-14
fix read temp line cout \n num ble cpp
題面
題目傳送門
解法
$\(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