ACM: 动态规划题 poj 2057 树状DP
来源:互联网 发布:金山tw域名遭抢注 编辑:程序博客网 时间:2024/06/06 03:03
When reaching the first fork of the tree, he sadly found that hecould not remember the route that he climbed before. In order tofind his lovely house, the snail decided to go to the end of everybranch. It was dangerous to walk without the protection of thehouse, so he wished to search the tree in the best way.
Fortunately, there lived many warm-hearted worms in the tree thatcould accurately tell the snail whether he had ever passed theirplaces or not before he fell off.
Now our job is to help the snail. We pay most of our attention totwo parts of the tree ---- the forks of the branches and the endsof the branches, which we call them key points because key eventsalways happen there, such as choosing a path, getting the help froma worm and arriving at the house he is searching for.
Assume all worms live at key points, and all the branches betweentwo neighboring key points have the same distance of 1. The snailis now at the first fork of the tree.
Our purpose is to find a proper route along which he can find hishouse as soon as possible, through the analysis of the structure ofthe tree and the locations of the worms. The only restriction onthe route is that he must not go down from a fork until he hasreached all the ends grown from this fork.
The house may be left at the end of any branches in an equalprobability. We focus on the mathematical expectation of thedistance the snail has to cover before arriving his house. We wishthe value to be as small as possible.
As illustrated in Figure-1, the snail is at the key point 1 and hishouse is at a certain point among 2, 4 and 5. A worm lives at point3, who can tell the snail whether his house is at one of point 4and 5 or not. Therefore, the snail can choose two strategies. Hecan go to point 2 first. If he cannot find the house there, heshould go back to point 1, and then reaches point 4 (or 5) by point3. If still not, he has to return point 3, then go to point 5 (or4), where he will undoubtedly find his house. In this choice, thesnail covers distances of 1, 4, 6 corresponding to thecircumstances under which the house is located at point 2, 4 (or5), 5 (or 4) respectively. So the expectation value is (1 + 4 + 6)/ 3 = 11 / 3. Obviously, this strategy does not make full use ofthe information from the worm. If the snail goes to point 3 andgets useful information from the worm first, and then chooses to goback to point 1 then towards point 2, or go to point 4 or 5 to takehis chance, the distances he covers will be 2, 3, 4 correspondingto the different locations of the house. In such a strategy, themathematical expectation will be (2 + 3 + 4) / 3 = 3, and it is thevery route along which the snail should search the tree.
Input
A test case of N = 0 indicates the end of input, and should not beprocessed.
Output
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
题意: 蜗牛要找回自己的家, 但是它忘记路线, 幸运的是每个树杈上会有worm告诉它在当前的树杈上,
解题思路:
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 1005
int n;
int num[MAX], flag[MAX];
int g[MAX][MAX];
int y[MAX], fail[MAX], result[MAX];
bool cmp(int u, int v)
{
}
void dfs(int u)
{
}
int main()
{
//
}
- ACM: 动态规划题 poj 2057 树状DP
- ACM: 树状DP 动态规划题 poj 1463 …
- ACM: dp动态规划题(难) poj 3267 …
- ACM: dp题(动态规划) poj 2151 (d…
- ACM: DP训练好题 动态规划题 poj 1…
- ACM: DP+floyd 动态规划题 poj 117…
- ACM: 概率DP 动态规划题 poj 1322
- ACM: 动态规划题 poj 1192 树形DP
- ACM: 简单动态规划题 poj 2955
- ACM: 动态规划题 poj 2241
- ACM: 动态规划题 poj 1054
- ACM: 动态规划题 poj 2948
- ACM: 动态规划题 poj 1925
- ACM: 动态规划题 poj 3034
- ACM: 动态规划题 poj 2411
- ACM: 动态规划题 poj 1947
- ACM: 动态规划题 poj 2486
- ACM: 动态规划题 poj 3140
- ACM: 动态规划题 poj 2411
- 埃拉托斯特尼筛法求素数
- 兴趣学习: 摆脱“三天打渔两天晒网”…
- ACM: 动态规划题 poj 1185 想了两…
- windows.h
- ACM: 动态规划题 poj 2057 树状DP
- ACM: 动态规划题 poj 1947
- ACM: 动态规划题 poj 2486
- ACM: 动态规划题 poj 3140
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 poj 1192 树形DP
- ACM: 动态规划题 poj 3659
- 动态规划: 经典0-1背包问题 (转)
- ACM: 动态规划题 poj&nb…