HDU-4035 Maze (probabilistic DP seeking expectation)

Source: Internet
Author: User
Tags cas

The main topic: in a tree-shaped maze, the room as a node. There are n rooms, each room has the probability of a trap for KI, there is an exit probability of EI, if both cases are not present (the probability of pi), then can only make a choice to move to the next room (including may go to the previous room). The root node is 1, and when a trap is encountered, it must return to the root node 1 and start again, and when it encounters an exit, go out of the maze. Ask for the desired number of choices made from the beginning to the maze.

Topic Analysis: Define the State DP (i) to indicate the desired number of choices at node I until out of the maze. The state transition equation is:

DP (i) =KI*DP (1) + (1/m) *pi*∑ (DP (son) +1) (I is leaf node) <1>

DP (i) =KI*DP (1) + (1/m) *pi* (DP (father) +1) + (1/m) *pi* ∑ (DP (son) +1) (I is a non-leaf node) <2>

Will <2> tidy up, get:

DP (i) =KI*DP (1) + (1/m) *PI*DP (father) + (1/m) *pi*∑dp (son) +pi

Obviously, DP (i) is related to DP (1), and DP (i) =a (i) *DP (1) +b (i) *DP (father) +c (i) <3>

Bring <3> into ∑DP (son) to get:

(pi/m) *∑a (son)) dp (i) = (ki+ (pi/m) ∑a (son)) *DP (1) + (pi/m) *DP (father) + (pi/m) *∑c (son) +pi

Obviously, A (i), B (i), C (i) are available.

The DP (1) =C (1)/(1-a (1)) is the answer.

The code is as follows:

# include<iostream># include<cstdio># include<cmath># include<vector># include<cstring ># include<algorithm>using namespace Std;const int n=10005;const int inf=100000;const double eps=1e-9;int N;    Double K[n];d ouble e[n];d ouble a[n],b[n],c[n];vector<int>g[n];bool dfs (int u,int fa) {int m=g[u].size ();    Double temp=0;    A[u]=k[u];    B[u]= (1-k[u]-e[u])/m;    C[u]=1-k[u]-e[u];        for (int i=0;i<m;++i) {int v=g[u][i];        if (V==FA) continue;        if (!dfs (V,u)) return false;        a[u]+= (1-k[u]-e[u]) *a[v]/m;        c[u]+= (1-k[u]-e[u]) *c[v]/m;    temp+= (1-k[u]-e[u]) *b[v]/m;    } if (Fabs (1-temp) <eps) return false;    A[u]/= (1-temp);    B[u]/= (1-temp);    C[u]/= (1-temp); return true;}    int main () {int t,x,y,cas=0;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i=1;i<=n;++i) g[i].clear ();            for (int i=1;i<n;++i) {scanf ("%d%d", &x,&y); G[X].push_back (y);        G[y].push_back (x);            } for (int i=1;i<=n;++i) {scanf ("%lf%lf", k+i,e+i);            k[i]/=100;        e[i]/=100;        } printf ("Case%d:", ++cas);        if (Dfs (1,-1) &&fabs (1-a[1]) >eps) printf ("%.6lf\n", c[1]/(1-a[1]));    else printf ("impossible\n"); } return 0;}

  

HDU-4035 Maze (probabilistic DP seeking expectation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.