[SHOI2014]概率充电器
Posted mrclr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SHOI2014]概率充电器相关的知识,希望对你有一定的参考价值。
嘟嘟嘟
这种复杂的概率大题我果然是每做出来……
然后我找到了一篇极棒的题解,小学生都能看懂(大佬就是大佬啊):题解 P4284 【[SHOI2014]概率充电器】,第二次dp的状态方程真的很妙啊。
刚开始我总按照套路想设\(dp[u]\)表示\(u\)的子树的期望,看完题解后发现这是没有依据的,因为每一个元件可以直接充电,不一定要形成依赖关系。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e5 + 5;
In ll read()
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
In void write(ll x)
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
In void MYFILE()
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
int n, a[maxn];
struct Edge
int nxt, to; db p;
e[maxn << 1];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y, int w)
e[++ecnt] = (Edge)head[x], y, 0.01 * w;
head[x] = ecnt;
db dp[maxn];
In void dfs1(int now, int _f)
dp[now] = 0.01 * a[now];
for(int i = head[now], v; ~i; i = e[i].nxt)
if((v = e[i].to) == _f) continue;
dfs1(v, now);
dp[now] = dp[now] + dp[v] * e[i].p - dp[now] * dp[v] * e[i].p;
db ans = 0;
In void dfs2(int now, int _f)
ans += dp[now];
for(int i = head[now], v; ~i; i = e[i].nxt)
if((v = e[i].to) == _f) continue;
db x = dp[v] * e[i].p;
if(x < 1)
db tp = (dp[now] - x) / (1.0 - x);
dp[v] = dp[v] + tp * e[i].p - dp[v] * tp * e[i].p;
dfs2(v, now);
int main()
//MYFILE();
Mem(head, -1);
n = read();
for(int i = 1; i < n; ++i)
int x = read(), y = read(), p = read();
addEdge(x, y, p), addEdge(y, x, p);
for(int i = 1; i <= n; ++i) a[i] = read();
dfs1(1, 0), dfs2(1, 0);
printf("%.6lf\n", ans);
return 0;
以上是关于[SHOI2014]概率充电器的主要内容,如果未能解决你的问题,请参考以下文章