【树形DP】【poj 2057】The Lost House
来源:互联网 发布:堆排序算法和其他的 编辑:程序博客网 时间:2024/05/16 12:11
http://poj.org/problem?id=2057
这道题很久以前就看到过,一直没看懂题,所以就一直放着
先学好英语,然后就可以做了
这篇论文里讲得很清楚
http://wenku.baidu.com/link?url=t2T0VjAbXPHk1tEM0o8f8orqAbDT34Xs0mAGDq4fxylFsla1RENJhrWgPmq6P7-DkhenpY9ASThFDKlplfTnf4jMdul4nFGtk2v8tEWO3nW
对于每个节点要贪心一遍决策,有序化了之后再DP
代码也写的很清楚了
//#define _TEST _TEST#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <cmath>#include <algorithm>using namespace std;/************************************************Code By willinglive Blog:http://willinglive.cf************************************************/#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)#define MS(arr,x) memset(arr,x,sizeof(arr))#define LL long long#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)inline const int read(){int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}/////////////////////////////////////////////////int n;struct edge{int v,next;}e[1010];int head[1010],k;bool flag[1010];struct node{int fa,fb,l;}T[1010];int a[1010];/////////////////////////////////////////////////void adde(int u,int v){e[k].v=v;e[k].next=head[u];head[u]=k++;}bool cmp(int a,int b){return (T[a].fb+2)*T[b].l < (T[b].fb+2)*T[a].l;}void dfs(int u){T[u].l=T[u].fa=T[u].fb=0;if(head[u]==-1) T[u].l=1;INE(i,u,e) dfs(e[i].v);*a=0;INE(i,u,e) a[++*a]=e[i].v; //这一句不能写在上面,否则浪费空间 if(a[0]){sort(&a[1],&a[*a+1],cmp);rep(i,1,*a){int v=a[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;}if(flag[u]) T[u].fb=0;}}/////////////////////////////////////////////////void input(){MS(head,-1); k=0;int u; char ch[3];rep(v,1,n){u=read(); scanf("%s",ch);if(~u) adde(u,v);flag[v]=ch[0]=='Y';}}void solve(){dfs(1);printf("%.4lf\n",(double)T[1].fa/T[1].l);}/////////////////////////////////////////////////int main(){ #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif while(scanf("%d",&n),n) input(),solve(); return 0;}
0 0
- 【树形DP】【poj 2057】The Lost House
- POJ 2057 The Lost House 树形DP+贪心
- POJ 2057 The Lost House 经典树形DP+贪心
- POJ 2057 The Lost House 树状DP
- 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
- POJ 2057 The Lost House
- poj-2057 The Lost House
- poj 2057 The Lost House
- pku 2057 The Lost House 树形dp+背包dp 解题报告
- 【Poj 2507】The Lost House(树型dp)
- POJ 2057 The Lost House 树型dp+贪心思想在动态规划上的应用
- 【树状数组】PKU-2057-The Lost House
- pku2057 The Lost House
- 排序算法
- ViewGroup selector属性问题...
- 如果你一天或几天都等不到他的电话或信息
- log4j学习资料
- 利用ProGuard进行代码混淆
- 【树形DP】【poj 2057】The Lost House
- hdu5074——Hatsune Miku
- PreferenceScreen 偏好显示类 的使用
- Android View Focus详解
- Maven使用小结
- UML类图几种关系的总结
- 第4章 考研求职两不误 开放-封闭原则 大话设计模式
- 林智仁LibSVM包的详细说明
- 与Costin Leau谈论Elasticsearch,大数据及Hadoop