POJ 2057 the Lost House

Source: Internet
Author: User
Tags float number uppercase character

The Lost housetime limit:3000msmemory limit:30000kbthis problem would be judged onPKU. Original id:2057
64-bit integer IO format: %lld Java class name: Main One day a snail climbed up to a big tree and finally came to the end of a branch. What a different feeling to look down from such a high place he had never been to before! However, he was very tired due to the long time of climbing, and fell asleep. An unbelievable thing happened if he woke up----he found himself lying in a meadow and his house originally on his BAC K disappeared! Immediately he realized that he fell off the branch when he is sleeping! He was sure, the He House must still is on the branch he had been sleeping on. The snail began to climb the tree again, since he could isn't live without his house.

When reaching the first fork of the tree, he sadly found that he could not remember the route that he climbed before. In order to find he lovely house, the snail decided to go to the end of the every branch. It is dangerous to walk without the protection of the House, so and he wished to search the "the best" of the tree.

Fortunately, there lived many warm-hearted worms in the tree that could accurately tell the snail whether he had ever pass Ed their places or not before he fell off.

Now we are job is to help the snail. We pay a very attention to the parts of the tree----The forks of the branches and the ends of the branches, which W E call them key points because key events all happen there, such as choosing a path, getting the help from a worm and a Rriving at the house he's searching for.

Assume all worms live at key points, and all the branches between both neighboring key points have the same distance of 1. The snail is now at the first fork of the tree.

Our purpose are to find a proper route along which he can find him house as soon as possible, through the analysis of the S Tructure of the tree and the locations of the worms. The only restriction on the route was that he must not go off from a fork until he had reached all the ends grown from thi S fork.

The house is branches to the end of any of the equal probability. We focus on the mathematical expectation of the distance the snail have to cover before arriving he house. We wish the value to be as small as possible.

As illustrated in Figure-1, the snail are at the key point 1 and he house are at a certain point among 2, 4 and 5. A worm lives at point 3, who can tell the snail whether he house was at one's point 4 and 5 or not. Therefore, the snail can choose and the strategies. He can go to point 2 first. If He cannot find the house there, he should go back to point 1, and then reaches point 4 (or 5) by point 3. If still not, he had to return point 3 and then go to point 5 (or 4), where he would undoubtedly find his house. In this choice, the snail covers distances of 1, 4, 6 corresponding to the circumstances under which the house is located At point 2, 4 (or 5), 5 (or 4) respectively. The expectation value is (1 + 4 + 6)/3 = 11/3. Obviously, this strategy does does the use of the information from the worm. If the snail goes to point 3 and gets useful information from the worm first, and then chooses to go back to point 1 then Towards point 2, or go-to-point 4 or 5-to-take his chance, the DISTances He covers would be 2, 3, 4 corresponding to the different locations of the house. In such a strategy, the mathematical expectation'll be (2 + 3 + 4)/3 = 3, and it's the very route along which the SNA Il should search the tree. 

InputThe input contains several sets of test data. Each set begins with a line containing one integer N, no more than, which indicates the number of key points in the T Ree. Then follow n lines describing the n key points. For convenience, we number all the key points from 1 to N. The key point, numbered with 1 are always the first fork of the tree. Other numbers is any key points in the tree except the first fork. The i-th line in these N lines describes the key point with number I. Each line consists of one integers and one uppercase character ' Y ' or ' N ' separated by a single space, which represents the Number of the previous key point and whether there lives a worm (' Y ' means lives and ' N ' means not). The previous key point means the neighboring key point in the shortest path between this key point and the key point Numbe Red 1. In the above illustration, the previous key point of a point 2 or 3 are point 1, while the previous key point of point 4 or 5 is point 3. This integer is-1 for The key point 1, means it have no previous key point. You can assume a fork have at the most eight branches. The first set in the sample input describes the above illustration. 

A test Case of N = 0 indicates the end of input, and should not to be processed.
OutputOutput one line for each set of input data. The line contains one float number with exactly four digits after the decimal point, which is the mathematical expectation Value.Sample Input
5-1 N1 N1 Y3 N3 n10-1 N1 Y1 N2 N2 N2 N3 N3 Y8 N8 n6-1 N1 N1 Y1 N3 N3 N0
Sample Output
3.00005.00003.5000
SourceBeijing 2004

Problem solving: greedy + Tree DP

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <algorithm>6 using namespacestd;7 Const intMAXN =1005;8vector<int>G[MAXN];9 intdp[maxn][2],son[maxn],n;Ten BOOLWORMS[MAXN]; One BOOLcmpConst intAConst intb) { A     return(dp[a][0] +2) *son[b] < (dp[b][0] +2)*Son[a]; - } - intDfsintu) { the     if(g[u].size () = =0) { -dp[u][0] = dp[u][1] =0; -         returnSon[u] =1; -     } +      for(inti = g[u].size ()-1; I >=0; -I.) SON[U] + =DFS (G[u][i]); - sort (G[u].begin (), G[u].end (), CMP); +      for(inti =0; I < g[u].size (); ++i) { Adp[u][1] + = dp[u][0]*son[g[u][i]] + dp[g[u][i]][1] +Son[g[u][i]]; atdp[u][0] + = dp[g[u][i]][0] +2; -     } -     if(Worms[u]) dp[u][0] =0; -     returnSon[u]; - } - intMain () { in     Charstr[5]; -      while(SCANF ("%d",&N), N) { to          for(inti =1; I <= N; ++i) g[i].clear (); +Memset (DP,0,sizeofDP); -Memset (Worms,false,sizeofworms); theMemset (son,0,sizeofson); *         intPre; $scanf"%d%s",&pre,str);Panax Notoginseng          for(inti =2; I <= N; ++i) { -scanf"%d%s",&pre,str); theWorms[i] = str[0] =='Y'; + G[pre].push_back (i); A         } theDfs1); +printf"%.4f\n",1. *dp[1][1]/son[1]); -     } $     return 0; $}
View Code

POJ 2057 the Lost House

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.