题解待会在上

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 2e4+11;
const double eps = 1e-10;
typedef long long ll;
const int oo = 0x3f3f3f3f;
const double ERR = -2.3333;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int to[maxn<<1],nxt[maxn<<1],head[maxn],tot;
void init(){
memset(head,-1,sizeof head);
tot=0;
}
void add(int u,int v){
to[tot]=v;nxt[tot]=head[u];head[u]=tot++;
swap(u,v);
to[tot]=v;nxt[tot]=head[u];head[u]=tot++;
}
double A[maxn],B[maxn],C[maxn],k[maxn],e[maxn];
bool isleaf(int u,int fa){
int cnt=0;
erep(i,u){
int v=to[i];
if(v!=fa)cnt++;
if(cnt==1)return 0;
}
return 1;
}
void DP(int u,int fa){
if(A[u]!=ERR)return;
if(isleaf(u,fa)){
A[u]=k[u];
B[u]=(1.0-e[u]-k[u]);
C[u]=(1.0-e[u]-k[u]);
return;
}
int num=0;
double sumA=0,sumB=0,sumC=0;
erep(i,u){
int v=to[i];
num++;
if(v==fa)continue;
DP(v,u);
sumA+=A[v];
sumB+=B[v];
sumC+=C[v];
}
int i=u;
A[i]=(k[i]+(1-k[i]-e[i])/num*sumA)/(1.0-(1.0-k[i]-e[i])/num*sumB);
B[i]=(1.0-k[i]-e[i])/num/(1.0-(1.0-k[i]-e[i])/num*sumB);
C[i]=((1.0-k[i]-e[i])+(1.0-k[i]-e[i])/num*sumC)/(1.0-(1.0-k[i]-e[i])/num*sumB); }
int main(){
int T=read(),kase=0;
while(T--){
init();
int n=read();
rep(i,1,n){
A[i]=B[i]=C[i]=ERR;
}
rep(i,1,n-1){
int u=read();
int v=read();
add(u,v);
}
rep(i,1,n){
scanf("%lf%lf",&k[i],&e[i]);
k[i]/=100.0;
e[i]/=100.0;
}
DP(1,0);
double ans=(fabs(1.0-A[1])<eps?ERR:C[1]/(1.0-A[1]));
printf("Case %d: ",++kase);
if(ans==ERR) printf("impossible\n");
else printf("%.6lf\n",ans);
}
return 0;
}

HDU - 4035 循环型概率DP的更多相关文章

  1. HDU 4035 Maze 概率dp,树形dp 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 求步数期望,设E[i]为在编号为i的节点时还需要走的步数,father为dfs树中该节点的父节点,son为 ...

  2. hdu 4035 Maze 概率DP

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

  3. HDU 4035 Maze 概率DP 搜索

    解题报告链接: http://www.cnblogs.com/kuangbin/archive/2012/10/03/2711108.html 先推公式,设计状态,令DP[i]表示在房间i退出要走步数 ...

  4. HDU 4035Maze(概率DP)

    HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp ...

  5. HDU 3853LOOPS(简单概率DP)

    HDU 3853    LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...

  6. poj 2096 Collecting Bugs && ZOJ 3329 One Person Game && hdu 4035 Maze——期望DP

    poj 2096 题目:http://poj.org/problem?id=2096 f[ i ][ j ] 表示收集了 i 个 n 的那个. j 个 s 的那个的期望步数. #include< ...

  7. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  8. HDU 4405 【概率dp】

    题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...

  9. HDU 4576 Robot(概率dp)

    题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...

随机推荐

  1. 217. Contains Duplicate数组重复元素 123

    [抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...

  2. Solidity字符串拼接实现oraclize动态查询

    solidity本身没有字符串拼接功能,但是如果你恰巧要用oraclize api,那么可以使用oraclize api中的字符串拼接方法(strConcat).之所以oraclize api里会有s ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. ASP.NET MVC5 Authentication Filters执行链

    注意区分认证和授权: The following  are the differences in short: Authentication(认证): It is a process of verif ...

  5. Redis主从服务部署

    Redis__WindowsServer主从服务部署及调用实例       一.先谈谈单个Redis服务的安装         使用的redis是2.8.17版本,从官网下载解压缩后文件内容为:   ...

  6. 关于modelsim闪退问题

    电脑之前做过仿真,modelsim是可以完美调用的,但是最近莫名其妙的就出现闪退问题,不通过quartus或者ise调用,单独使用的时候也会闪退. 偶尔一次能抓到错误消息.如下图所示: 然并卵,网上根 ...

  7. Js杂谈-插件包读后感

    最近有幸得到了一份项目上的前端封装的插件库代码,花了一个下午时间,仔细地研读了一下.对于我很想做自己的类库,搞自己的组件包很有启蒙意义. 相比较我之前阅过的框架或是类库,这份比较简单. 项目是jQue ...

  8. 【转】Android自定义控件(三)——有弹性的ListView

    原文地址:http://blog.csdn.net/a105865708/article/details/17959459 上一次我们试验了有弹性的ScrollView.详情 这一次,我们来试验有弹性 ...

  9. Echarts+WPF

    C# using System; using System.Collections.Generic; using System.IO; using System.Linq; using System. ...

  10. C#构造函数用法

    1.实例构造函数 2.静态构造函数 3.私有构造函数 例: 创建一个类 using System; using System.Collections.Generic; using System.Lin ...