HDU 4035 Maze 期望dp
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4035
Maze
Memory Limit: 65768/65768 K (Java/Others)
样例输出
Case 1: 2.000000
Case 2: impossible
Case 3: 2.895522
题意
给你一颗树,你在1号点,每次你会随机选择一个相邻的点走过去(走过的点也可以再走),每个点都有ki的概率被打回1号点,也有ei的可能逃出去。
现在问这个人逃出去要经过的期望次数是多少。
题解
高斯消元要n^3,吃不消,不过可以通过公式转化,解开环。
[port]
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=10101;
double dp[maxn],ex[maxn],ki[maxn];
int n;
VI G[maxn];
double A[maxn],B[maxn],C[maxn];
bool dfs(int u,int fa){
bool child=false;
int m=G[u].sz();
double sa=0,sb=0,sc=0;
rep(i,0,G[u].sz()){
int v=G[u][i];
if(v==fa) continue;
child=true;
bool su=dfs(v,u);
if(!su) return su;
sa+=A[v],sb+=B[v],sc+=C[v];
}
if(!child){
A[u]=ki[u];
C[u]=B[u]=1-ki[u]-ex[u];
}else{
double tmp=(1-ki[u]-ex[u])/m;
if(fabs(1-tmp*sb)<eps) return false;
A[u]=(ki[u]+tmp*sa)/(1-tmp*sb);
B[u]=tmp/(1-tmp*sb);
C[u]=(1-ki[u]-ex[u]+tmp*sc)/(1-tmp*sb);
}
return true;
}
void init(){
for(int i=1;i<=n;i++) G[i].clear();
}
int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
init();
rep(i,0,n-1){
int u,v;
scf("%d%d",&u,&v);
G[u].pb(v);
G[v].pb(u);
}
for(int i=1;i<=n;i++){
scf("%lf%lf",&ki[i],&ex[i]);
ki[i]/=100;
ex[i]/=100;
}
bool su=dfs(1,-1);
prf("Case %d: ",++kase);
if(fabs(1-A[1])<eps||!su) puts("impossible");
else prf("%.6lf\n",C[1]/(1-A[1]));
}
return 0;
}
//end-----------------------------------------------------------------------