HDU 4035 Maze 概率dp+树形dp

题解:点击打开链接

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
const double eps = 1e-9;
const int N = 10010;
vector<int> G[N];
int n;
double k[N], e[N], dp[N];
double A[N], B[N], C[N];
bool dfs(int u, int fa){
	int m = G[u].size();
	A[u] = k[u];
	B[u] = (1 - k[u] - e[u])/m;
	C[u] = 1 - k[u] - e[u];
	double tmp = (1 - k[u] - e[u]) / (double)m, self = 0;
	for(int i = G[u].size()-1; i >= 0; i--){
		int v = G[u][i]; if(v == fa)continue;
		if(!dfs(v, u)) return false;
		A[u] +=  tmp * A[v];
		self += tmp * B[v];
		C[u] += tmp * C[v];
	}
	if(fabs(self - 1.0) < eps) return false;
	A[u] /= 1-self;
	B[u] /= 1-self;
	C[u] /= 1-self;
	return true;
}

void input(){
	cin>>n;
	for(int i = 1, u, v; i < n; i++){
		scanf("%d %d", &u, &v);
		G[u].push_back(v); G[v].push_back(u);
	}
	for(int i = 1; i <= n; i++) {
		scanf("%lf %lf", &k[i], &e[i]);
		k[i] /= 100.0;
		e[i] /= 100.0;
	}
}
int main() {
	int T, Cas = 1; cin>>T;
    while(T--){
    	input();
    	bool ok = dfs(1, -1);

    	printf("Case %d: ", Cas++);
    	if(!ok || fabs(1.0-A[1]) < eps)
    		puts("impossible");
    	else
    		printf("%.6f\n", C[1] / (1.0-A[1]));

    	for(int i = 1; i <= n; i++) G[i].clear();
    }
    return 0;
}
时间: 2024-10-12 19:09:36

HDU 4035 Maze 概率dp+树形dp的相关文章

hdu 4035 Maze (概率DP)

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1713    Accepted Submission(s): 659 Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted b

HDU 4035 Maze 概率DP 好题

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 802Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted by

HDU 4035 Maze 概率dp 难度:2

http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为dfs树种该节点的子节点的集合,kl[i]为被杀掉的概率,ex[i]为逃出的概率 mv[i]=(1-kl[i]-ex[i])/(1+len(son)) 则明显 E[i]=(E[father]+1)*mv[i]+sigma((E[son]+1)*mv[i])+E[1]*K[i] 未知量是E[i],E[

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

bzoj 3566: [SHOI2014]概率充电器 树形DP

首先普及一个概率公式 P(A+B)=P(A)+P(B)-P(AB) 题意:一些充电元件和导线构成一棵树,充电元件是否能充电有2种情况, 1.它自己有qi%的概率充电 2.与它相邻的元件通过导线给它充电(导线有p%的概率导通) 求最终充了电的元件的期望 题解:首先可以将元件能否充电分成3种情况考虑, 1.它自己给自己充好了电 2.它的儿子方向给它传送了电 3.它的父亲方向给它传送了电. 对于1,题目已经给出可以直接赋值, 对于2,可以通过一次树的深度遍历求得.pson[now]=pson[now]

hdu 4118 Holiday&#39;s Accommodation 树形dp

Holiday's Accommodation Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4118 Description Nowadays, people have many ways to save money on accommodation when they are on vacation.One of these ways is exchanging

hdu 5379 Mahjong tree(树形dp)

题目链接:hdu 5379 Mahjong tree 树形dp,每个节点最多有2个子节点为一棵节点数大于1的子树的根节点,而且要么后代的节点值都大于,要么都小于本身(所以tson不为0是,要乘2).对于K个单一节点的子节点,种类数即为全排K!.当一个节点没有兄弟节点时,以这个节点为根结点的子树,根可以选择最大或者最小. #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

HDU 5758 Explorer Bo(树形DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次数尽量少的情况下至少经过每条路一遍啊,同时最小化走过的路程总长度.输出路程总长度. [题解] 首先,对于传送次数尽量少这个条件,我们很容易发现,当且仅当每次出发点和终止点都是叶节点的时候,是最少的,当然在叶节点无法两两匹配的时候,再多走一条链. 然后就是叶节点的匹配问题,使得匹配后的叶节点连线覆盖全