传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4035

真的是一道好题,题解比较麻烦,我自己在纸上写了好大一块草稿才搞出来,不用公式编辑器的话就很难看清楚,所以不上题解啦,贴一个题解的链接:http://blog.csdn.net/balloons2012/article/details/7891054

注意此题卡精度,我一开始eps是1e-8,WA掉了,开到了1e-10,AC~,真是烦卡精度的题。

#include <cstdio>
#include <cstring>
#include <cmath> const int maxn = 20005; int T, n, t1, t2, k[maxn], e[maxn];
int head[maxn], to[maxn << 1], next[maxn << 1], lb, out[maxn];
double a[maxn], b[maxn], c[maxn]; inline void ist(int aa, int ss) {
to[lb] = ss;
next[lb] = head[aa];
head[aa] = lb;
++lb;
++out[aa];
}
bool dfs(int r, int p) {
if (r > 1 && out[r] == 1) {
a[r] = (double)k[r] / 100.0;
b[r] = c[r] = (double)(100 - k[r] - e[r]) / 100.0;
return true;
}
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != p) {
dfs(to[j], r);
a[r] += a[to[j]];
b[r] += b[to[j]];
c[r] += c[to[j]];
}
}
double tem = 1.0 - (100 - k[r] - e[r]) * b[r] / 100.0 / out[r];
if (fabs(tem) < 1e-10) {
return false;
}
a[r] = ((k[r] / 100.0) + (100 - k[r] - e[r]) * a[r] / 100.0 / out[r]) / tem;
b[r] = (100 - k[r] - e[r]) / 100.0 / out[r] / tem;
c[r] = ((100 - k[r] - e[r]) / 100.0 + (100 - k[r] - e[r]) * c[r] / 100.0 / out[r]) / tem;
return true;
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d", &T);
for (int kase = 1; kase <= T; ++kase) {
printf("Case %d: ", kase);
memset(head, -1, sizeof head);
memset(next, -1, sizeof next);
lb = 0;
memset(out, 0, sizeof out);
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
memset(c, 0, sizeof c);
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &t1, &t2);
ist(t1, t2);
ist(t2, t1);
}
for (int i = 1; i <= n; ++i) {
scanf("%d%d", k + i, e + i);
} if (dfs(1, 0) && fabs(1.0 - a[1]) > 1e-10) {
printf("%f\n", c[1] / (1.0 - a[1]));
}
else {
puts("impossible");
}
}
return 0;
}

  

[hdu4035] Maze【概率dp 数学期望】的更多相关文章

  1. hdu4035 Maze (树上dp求期望)

    dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 ...

  2. HDU4035 Maze (概率DP)

    转:https://www.cnblogs.com/kuangbin/archive/2012/10/03/2711108.html 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, ...

  3. bzoj1415 [Noi2005]聪聪和可可【概率dp 数学期望】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1415 noip2016 D1T3,多么痛的领悟...看来要恶补一下与期望相关的东西了. 这是 ...

  4. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  5. [poj2096] Collecting Bugs【概率dp 数学期望】

    传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...

  6. [hdu4089] Activation【概率dp 数学期望】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4089 本来可以一遍过的,结果mle了一发...注意要用滚动数组. 令f(i, j)表示队列剩余i个人,这 ...

  7. [poj3744] Scout YYF I【概率dp 数学期望】

    传送门:http://poj.org/problem?id=3744 令f(i)表示到i,安全的概率.则f(i) = f(i - 1) * p + f(i - 2) * (1 - p),若i位置有地雷 ...

  8. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  9. poj 2096 Collecting Bugs (概率dp 天数期望)

    题目链接 题意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcom ...

随机推荐

  1. java下XML与JSON互相转换的Utils类

    原文:http://heipark.iteye.com/blog/1394844 需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下: < ...

  2. 主成分分析(principal components analysis)

    http://www.cnblogs.com/jerrylead/tag/Machine%20Learning/ PCA的思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征.这k维特 ...

  3. 纠结的链接——ln、ln -s、fs.symlink、require

    纠结的链接--ln.ln -s.fs.symlink.require 提交 我的留言 加载中 已留言 inode 我们首先来看看 linux 系统里面的一个重要概念:inode. 我们知道,文件存储在 ...

  4. STL 源代码剖析 算法 stl_algo.h -- nth_element

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...

  5. 在EasyUI的DataGrid中嵌入Combobox

    在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...

  6. 嵌入式开发之davinci---dm8127 ipipe

    http://blog.csdn.net/dog0138/article/details/4212576 http://e2e.ti.com/support/dsp/davinci_digital_m ...

  7. OpenCV基本图像容器Mat的几种创建方法

    參考文章:http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html 实验说明: (引用) 本文主要讲一些opencv 2.0 ...

  8. oracle连接串的一种写法

    我在.NET项目里访问oracle,向来是规规矩矩地这样写: DATA SOURCE=PDBGZFBC;PASSWORD=test;PERSIST SECURITY INFO=True;USER ID ...

  9. 2015/12/29 eclipse应用 输出三角形

    public class Myfirst { public static void main(String[] args) { System.out.println("hello world ...

  10. C# 敏感词过滤

    public class BadWordFilter { #region 变量 private HashSet<string> hash = new HashSet<string&g ...