首页 > 代码库 > POJ 2057 The Lost House
POJ 2057 The Lost House
The Lost House
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 2140 | Accepted: 886 |
Description
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 when he woke up ---- he found himself lying in a meadow and his house originally on his back disappeared! Immediately he realized that he fell off the branch when he was sleeping! He was sure that his house must still be on the branch he had been sleeping on. The snail began to climb the tree again, since he could not 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 his lovely house, the snail decided to go to the end of every branch. It was dangerous to walk without the protection of the house, so he wished to search the tree in the best way.
Fortunately, there lived many warm-hearted worms in the tree that could accurately tell the snail whether he had ever passed their places or not before he fell off.
Now our job is to help the snail. We pay most of our attention to two parts of the tree ---- the forks of the branches and the ends of the branches, which we call them key points because key events always happen there, such as choosing a path, getting the help from a worm and arriving at the house he is searching for.
Assume all worms live at key points, and all the branches between two neighboring key points have the same distance of 1. The snail is now at the first fork of the tree.
Our purpose is to find a proper route along which he can find his house as soon as possible, through the analysis of the structure of the tree and the locations of the worms. The only restriction on the route is that he must not go down from a fork until he has reached all the ends grown from this fork.
The house may be left at the end of any branches in an equal probability. We focus on the mathematical expectation of the distance the snail has to cover before arriving his house. We wish the value to be as small as possible.
As illustrated in Figure-1, the snail is at the key point 1 and his house is at a certain point among 2, 4 and 5. A worm lives at point 3, who can tell the snail whether his house is at one of point 4 and 5 or not. Therefore, the snail can choose two 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 has to return point 3, then go to point 5 (or 4), where he will 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. So the expectation value is (1 + 4 + 6) / 3 = 11 / 3. Obviously, this strategy does not make full 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 will be 2, 3, 4 corresponding to the different locations of the house. In such a strategy, the mathematical expectation will be (2 + 3 + 4) / 3 = 3, and it is the very route along which the snail should search the tree.
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 his lovely house, the snail decided to go to the end of every branch. It was dangerous to walk without the protection of the house, so he wished to search the tree in the best way.
Fortunately, there lived many warm-hearted worms in the tree that could accurately tell the snail whether he had ever passed their places or not before he fell off.
Now our job is to help the snail. We pay most of our attention to two parts of the tree ---- the forks of the branches and the ends of the branches, which we call them key points because key events always happen there, such as choosing a path, getting the help from a worm and arriving at the house he is searching for.
Assume all worms live at key points, and all the branches between two neighboring key points have the same distance of 1. The snail is now at the first fork of the tree.
Our purpose is to find a proper route along which he can find his house as soon as possible, through the analysis of the structure of the tree and the locations of the worms. The only restriction on the route is that he must not go down from a fork until he has reached all the ends grown from this fork.
The house may be left at the end of any branches in an equal probability. We focus on the mathematical expectation of the distance the snail has to cover before arriving his house. We wish the value to be as small as possible.
As illustrated in Figure-1, the snail is at the key point 1 and his house is at a certain point among 2, 4 and 5. A worm lives at point 3, who can tell the snail whether his house is at one of point 4 and 5 or not. Therefore, the snail can choose two 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 has to return point 3, then go to point 5 (or 4), where he will 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. So the expectation value is (1 + 4 + 6) / 3 = 11 / 3. Obviously, this strategy does not make full 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 will be 2, 3, 4 corresponding to the different locations of the house. In such a strategy, the mathematical expectation will be (2 + 3 + 4) / 3 = 3, and it is the very route along which the snail should search the tree.
Input
The input contains several sets of test data. Each set begins with a line containing one integer N, no more than 1000, which indicates the number of key points in the tree. 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 is always the first fork of the tree. Other numbers may be 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 integer 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 numbered 1. In the above illustration, the previous key point of point 2 or 3 is 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 has no previous key point. You can assume a fork has at 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 be processed.
A test case of N = 0 indicates the end of input, and should not be processed.
Output
Output 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 N 1 N 1 Y 3 N 3 N 10 -1 N 1 Y 1 N 2 N 2 N 2 N 3 N 3 Y 8 N 8 N 6 -1 N 1 N 1 Y 1 N 3 N 3 N 0
Sample Output
3.0000 5.0000 3.5000
Source
Beijing 2004
一道很经典的树形dp,想了很长时间,脑子里思路太乱了,主要纠结在子节点按什么顺序来选取上,原来可以贪心的。其他的思路也乱起八糟的,无奈之余看了解题报告,看了之后瞬间清醒了,救正了自己很多误区
le[N] :存取节点的叶子节点数目
fa[x]: 存取在根节点x的时候不能成功访问要走的步数
su[x]:存取在根节点x的时候成功访问各个叶子节点所需要的步数和
那么最终的结果为su[1]/le[1]
关系式: fa[x]+=fa[y]+2; 2为x到y一去一回
su[x]+=(fa[x]+1)*le[y]+su[y]; fa[x]中的x并不是完全根节点x不能成功访问的步数 和,而是不断更新的,在访问y之前,肯定访问了y的很多兄弟,访问这些兄弟都失败,后然后访问y。
贪心排序:
bool cmp(int p1,int p2)
{
return (fa[p1]+2)*le[p2]<(fa[p2]+2)*le[p1];
}
先访问p1 后访问p2,和先访问p2 后访问p1,哪个更小,化简后就能得出上面这个关系式。
其实自我感觉如果真想不出这样排序的话,可以用二进制dp一下,应该也是可以实现的。
一道很经典的树形dp,想了很长时间,脑子里思路太乱了,主要纠结在子节点按什么顺序来选取上,原来可以贪心的。其他的思路也乱起八糟的,无奈之余看了解题报告,看了之后瞬间清醒了,救正了自己很多误区
le[N] :存取节点的叶子节点数目
fa[x]: 存取在根节点x的时候不能成功访问要走的步数
su[x]:存取在根节点x的时候成功访问各个叶子节点所需要的步数和
那么最终的结果为su[1]/le[1]
关系式: fa[x]+=fa[y]+2; 2为x到y一去一回
su[x]+=(fa[x]+1)*le[y]+su[y]; fa[x]中的x并不是完全根节点x不能成功访问的步数 和,而是不断更新的,在访问y之前,肯定访问了y的很多兄弟,访问这些兄弟都失败,后然后访问y。
贪心排序:
bool cmp(int p1,int p2)
{
return (fa[p1]+2)*le[p2]<(fa[p2]+2)*le[p1];
}
先访问p1 后访问p2,和先访问p2 后访问p1,哪个更小,化简后就能得出上面这个关系式。
其实自我感觉如果真想不出这样排序的话,可以用二进制dp一下,应该也是可以实现的。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> #define N 1100 using namespace std; struct num { int y,next; }a[N*2]; int b[N]; bool status[N],ch[N]; char str[10]; int su[N],fa[N],le[N],Top; bool cmp(int p1,int p2) { return (fa[p1]+2)*le[p2]<(fa[p2]+2)*le[p1]; } int main() { //freopen("data.txt","r",stdin); void addeage(int x,int y); void dfs(int u); int n; while(scanf("%d",&n)!=EOF) { if(n==0) { break; } Top = 0; memset(b,-1,sizeof(b)); memset(status,false,sizeof(status)); for(int i=1;i<=n;i++) { int x; scanf("%d %s",&x,str); if(x!=-1&&i!=x) { addeage(i,x); addeage(x,i); } if(str[0]=='Y') { status[i] = true; } } memset(ch,false,sizeof(ch)); memset(fa,0,sizeof(fa)); memset(le,0,sizeof(le)); memset(su,0,sizeof(su)); dfs(1); double ans = (double)su[1]/(double)le[1]; printf("%.4lf\n",ans); } return 0; } void addeage(int x,int y) { a[Top].y = y; a[Top].next = b[x]; b[x] = Top++; } void dfs(int u) { ch[u] = true; bool c = true; for(int i=b[u];i!=-1;i=a[i].next) { int y = a[i].y; if(!ch[y]) { c = false; break; } } if(c) { fa[u] = 0; le[u] = 1; su[u] = 0; return ; } int w[N],sum=0; for(int i=b[u];i!=-1;i=a[i].next) { int y = a[i].y; if(!ch[y]) { dfs(y); w[sum++] = y; le[u]+=le[y]; } } sort(w,w+sum,cmp); for(int i=0;i<=sum-1;i++) { int y = w[i]; su[u]+=(fa[u]+1)*le[y]+su[y]; fa[u]+=(fa[y]+2); } if(status[u]) { fa[u] = 0; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。