"HDU" 4035 Maze

Source: Internet
Author: User

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

Test instructions: to a tree of n nodes, each node has a value of k[i] and e[i], respectively, the probability of k[i] to 1th nodes, E[i] probability to win (that is, stop), if there is no action on the top, then the probability of the trend and the node edge point. Ask the desired side of the passing. (n<=10000)

#include <cstdio> #include <cstring>using namespace std;const int n=10005;const double eps=1e-10;struct dat { Double A, B, C, K, E; }d[n];struct E {int next, to;} E[n<<1];int N, CNT, ihead[n];void CLR () {memset (ihead, 0, sizeof (int) * (n+1)); cnt=0;} void Add (int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt]. To=u;} Double abs (double x) {return x<0?-x:x;} void Dfs (int x, int fa) {bool flag=1;double temp=1-d[x]. K-D[X]. E, Suma=0, sumb=0, sumc=0;int m=0;for (int i=ihead[x]; i; ++m, I=e[i].next) if (E[I].TO!=FA) {Flag=0;dfs (e[i].to, x); suma+= D[E[I].TO].A;SUMB+=D[E[I].TO].B;SUMC+=D[E[I].TO].C;} if (flag) {d[x].a=d[x]. K D[x].b=d[x].c=temp; Return }double di=m-temp*sumb;d[x].a= (D[x]. K*m+temp*suma)/di;d[x].b=temp/di;d[x].c=temp* (sumc+m)/di;} void work () {DFS (1,-1), if (ABS (1-D[1].A) <=eps) puts ("impossible"), Else printf ("%.6f\n", d[1].c/(1-D[1].A));} int main () {int T; scanf ("%d", &t); for (int tt=1; tt<=t; ++TT) {sCANF ("%d", &n), for (int i=0; i<n-1; ++i) {int x, y; scanf ("%d%d", &x, &y); Add (x, y);} for (int i=1; i<=n; ++i) scanf ("%lf%lf", &d[i]. K, &d[i]. E), D[i]. K/=100, D[i]. e/=100;printf ("Case%d:", TT); work (); CLR ();} return 0;}

  

Good God's problem ... The feeling of making this question has deepened the understanding of the expectation greatly ...

The state $e[i]$ indicates that the current $i$ node also needs to go $e[i]$ the desired edge to end, the answer is $e[1]$, easy to get:

For leaf nodes:

$$
\begin{align}
E[i] & = k[i]e[1]+e[i]*0+ (1-k[i]-e[i]) * (e[father]+1) \ \
& = k[i]e[1]+ (1-k[i]-e[i]) e[father]+ (1-k[i]-e[i])
\end{align}
$$

For non-leaf nodes:

$$
\begin{align}
E[i] & = k[i]e[1]+e[i]*0+\frac{(1-k[i]-e[i]) (e[father]+1+\sum_{j is I child} (e[j]+1)}{m} \ \
& = k[i]e[1]+\frac{1-k[i]-e[i]}{m}e[father] + \frac{1-k[i]-e[i]}{m} \sum_{j is I child} E[j] + (1-k[i]-e[i])
\end{align}
$$

Because it is a tree, we have to dig deep into their nature:

1, we found that the non-leaf node is just more $\sum_{j is I child} e[j]$

2, because the expectation is originally to seek the limit significance, if the naked such request is obviously recursive infinite, we need to pass the mathematical skill to lose the formula

For example we can for convenience, set $e[i]$ general formula for $e[i]=a[i]e[1]+b[i]e[Father]+c[i]$ (why take a father unknown?) That's because $i$ 's son needs to know the expectations of his father (ie, $i$), so we can solve the $e[i]$ by bringing them into the original and moving them, isn't it amazing? 0.0)

So $\sum_{j is I child} e[j] = \sum_{j is I child} \left (A[j]e[1] + b[j]e[i] + c[j] \right) $

The final simplification of the super-strong formula, to find the coefficient of the general equation! :

For leaf nodes:

$ $A [I]=k[i], b[i]=c[i]=1-k[i]-e[i]$$

For non-leaf nodes:

Set $di=m-(1-k[i]-e[i]) \sum_{j} B[j], m= the number of nodes that are connected to the I edge $

$$
\begin{align}
A[i] & = (k[i]m+ (1-k[i]-e[i]) \sum_{j} A[j])/di \ \
B[i] & = (1-k[i]-e[i])/di \ \
C[i] & = (1-k[i]-e[i]) (M+\sum_{j} c[j])/di
\end{align}
$$

Final Answer:

$E [1]=c[i]/(1-a[1]) $, to determine whether the divisor is 0 can be judged that there is no solution.

"HDU" 4035 Maze

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.