BZOJ 3566 SHOI 2014 概率充电器 概率DP

题目大意:给出一棵树,每个节点都有一个充电概率,每一条边有一个导电概率,求期望有多少个点充电。


思路:写不出题解,粘一个详细的地址:http://wyfcyx.is-programmer.com/posts/74623.html


CODE:


#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define EPS 1e-8
#define MAX 500010
using namespace std;

int points;
double src[MAX];

int head[MAX],total;
int _next[MAX << 1],aim[MAX << 1];
double length[MAX << 1];
int father[MAX];

double f[MAX][2],p[MAX];

inline void Add(int x,int y,double len)
{
	_next[++total] = head[x];
	aim[total] = y;
	length[total] = len;
	head[x] = total;
}

void DFS1(int x,int last)
{
	f[x][0] = 1.0 - src[x];
	father[x] = last;
	for(int i = head[x]; i; i = _next[i]) {
		if(aim[i] == last)	continue;
		p[aim[i]] = length[i];
		DFS1(aim[i],x);
		f[x][0] *= (f[aim[i]][0] + (1 - f[aim[i]][0]) * (1 - length[i]));
	}
}

void DFS2(int x,int last)
{
	for(int i = head[x]; i; i = _next[i]) {
		if(aim[i] == last)	continue;
		double t = f[aim[i]][0] + (1 - f[aim[i]][0]) * (1 - length[i]);
		double _p = t < EPS ? 0:f[x][1] * f[x][0] / t;
		f[aim[i]][1] = _p + (1 - _p) * (1 - length[i]);
		DFS2(aim[i],x);
	}
}

int main()
{
	cin >> points;
	for(int x,y,z,i = 1; i < points; ++i) {
		scanf("%d%d%d",&x,&y,&z);
		Add(x,y,(double)z / 100.0),Add(y,x,(double)z / 100.0);
	}
	for(int i = 1; i <= points; ++i) {
		scanf("%lf",&src[i]);
		src[i] /= 100.0;
	}
	DFS1(1,0);
	f[1][1] = 1;
	DFS2(1,0);
	double ans = .0;
	for(int i = 1; i <= points; ++i)
		ans += (1 - f[i][0] * f[i][1]);
	cout << fixed << setprecision(6) << ans << endl;
	return 0;
}




你可能感兴趣的:(bzoj,概率DP,树形DP,SHOI2014)