poj 2057 The Lost House 贪心思想在动态规划上的应用
2013-01-16 22:16
411 查看
浅谈贪心思想在动态规划中的应用 ----- 黄劲松 论文中的例题,其中也附有详接
讲讲自己的理解吧.
题目所求为, 使用某种特定策略,使从根节点rt ,依次到每个叶子节点最少步长. 再除以叶子节点数量. 即为最终期望值.
对于一个以 rt 为根的子树上, 我们将其分为 House在子树rt上的步长总数Fa , 以及不在子树上步长总数Fb:
View Code
讲讲自己的理解吧.
题目所求为, 使用某种特定策略,使从根节点rt ,依次到每个叶子节点最少步长. 再除以叶子节点数量. 即为最终期望值.
对于一个以 rt 为根的子树上, 我们将其分为 House在子树rt上的步长总数Fa , 以及不在子树上步长总数Fb:
View Code
// code by: yefeng1627 // time: 2013-1-16 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int N = 1010; struct node { int fa,fb,L; bool flag; }T ; vector<int> Q ; int n; bool cmp( int x, int y ) {return ( (T[x].fb+2)*T[y].L < (T[y].fb+2)*T[x].L );} void dfs( int u ) {// return the fa(u) // 首先得到 u 的所有子节点信息 for(int i = 0; i < (int)Q[u].size(); i++) dfs( Q[u][i] ); // 初始化 T[u].fa = T[u].fb = T[u].L = 0; // 叶子节点 if( Q[u].size() == 0 ) T[u].L = 1; if( Q[u].size() > 0 ){ sort( Q[u].begin(), Q[u].end(), cmp ); for(int i = 0; i < Q[u].size(); i++) { int v = Q[u][i]; T[u].fa += ( T[u].fb+1 )*T[v].L + T[v].fa; T[u].fb += T[v].fb+2; T[u].L += T[v].L; } // 若节点u 上存在毛毛虫,则返回花费0 if( T[u].flag ) T[u].fb = 0; } } int main() { while( scanf("%d", &n) , n ) { for(int i = 0; i <= n; i++) Q[i].clear(); char str[2]; for(int x = 1; x <= n; x++) { int pre; scanf("%d %s", &pre, str); if( pre != -1 ) Q[pre].push_back(x); T[x].flag = ( (str[0]=='Y') ? true : false ); } dfs(1); double ans = 1.0*T[1].fa/T[1].L; printf("%.4f\n", ans); // printf("Fa(rt) = %d\n", T[1].fa ); } return 0; }
相关文章推荐
- POJ-2057 The Lost House 贪心在动态规划中的应用
- POJ 2057 The Lost House 树型dp+贪心思想在动态规划上的应用
- POJ 2057 贪心思想在动态规划中的应用
- POJ 2057 The Lost House 经典树形DP+贪心
- poj 2287 Tian Ji -- The Horse Racing 贪心思想在动态规划上的应用
- POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
- POJ 2057 The lost house
- POJ 2057 The Lost House (经典树形dp)
- POJ 2057 The Lost House
- POJ 2057 The Lost House
- POJ 2057 The Lost House
- POJ 2057 The Lost House [树状DP]
- POJ 2057 The Lost House
- poj 2057 The Lost House
- POJ 2057 The Lost House 树状DP
- poj_2057 The Lost House(树形dp)
- POJ 2057 The Lost House
- poj 2057 The Lost House
- poj 2057 The Lost House
- POJ 2057 The Lost House