题目链接:  POJ 1155 TELE

分析:  用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理.

       dp[cnt][i+j] = max( dp[cnt][i+j] , dp[cnt][i]+dp[son][j]-pay );

      其中pay是cnt->son这一路径的成本

代码: 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x7FFFFFFF;
const int maxn = 3005; struct node{
int to;
int pay;
node* next;
}tree[maxn],*head[maxn]; int w[maxn],num[maxn],tmp[maxn];
int dp[maxn][maxn];
int n,m,ptr; void Init(){
ptr=1;
memset(num,0,sizeof(num));
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
dp[i][j]=-inf;
} void AddEdge(int a,int b,int c){
tree[ptr].to=b;
tree[ptr].pay=c;
tree[ptr].next=head[a];
head[a]=&tree[ptr++];
} void DFS(int cnt){
dp[cnt][0]=0;
if(cnt>n-m){
num[cnt]=1;
dp[cnt][1]=w[cnt];
return ;
}
node* p=head[cnt];
while(p!=NULL){
DFS(p->to);
for(int i=0;i<=num[cnt];++i)
tmp[i]=dp[cnt][i]; ///不能放到下面的循环里, 因为可能有些会改变
for(int i=0;i<=num[cnt];++i)
for(int j=1;j<=num[p->to];++j)
dp[cnt][i+j]=max(dp[cnt][i+j],tmp[i]+dp[p->to][j]-p->pay);
num[cnt]+=num[p->to]; ///cnt的子结点最多能选用户的数量
p=p->next;
}
} int main(){
while(~scanf("%d%d",&n,&m)){
Init();
for(int i=1;i<=n-m;++i){
int t; scanf("%d",&t);
while(t--){
int a,b; scanf("%d%d",&a,&b);
AddEdge(i,a,b);
}
}
for(int i=n-m+1;i<=n;++i)
scanf("%d",w+i);
DFS(1);
for(int i=m; i>=0; --i)
if(dp[1][i]>=0) {
printf("%d\n",i); break;
}
}
return 0;
}

POJ 1155 树形背包(DP) TELE的更多相关文章

  1. poj 1155 树形背包

    http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...

  2. POJ 2486 树形背包DP Apple Tree

    设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...

  3. [POJ1155]TELE(树形背包dp)

    看到这道题的第一眼我把题目看成了TLE 哦那不是重点 这道题是树形背包dp的经典例题 题目描述(大概的): 给你一棵树,每条边有一个cost,每个叶节点有一个earn 要求在earn的和大于等于cos ...

  4. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  5. 【bzoj4007】[JLOI2015]战争调度 暴力+树形背包dp

    题目描述 给你一棵 $n$ 层的完全二叉树,每个节点可以染黑白两种颜色.对于每个叶子节点及其某个祖先节点,如果它们均为黑色则有一个贡献值,如果均为白色则有另一个贡献值.要求黑色的叶子节点数目不超过 $ ...

  6. 【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp

    题目描述 给出一个有 $2^n$ 个叶子节点的完全二叉树.每个叶子节点可以选择黑白两种颜色. 对于每个非叶子节点左子树中的叶子节点 $i$ 和右子树中的叶子节点 $j$ :如果 $i$ 和 $j$ 的 ...

  7. 【bzoj4987】Tree 树形背包dp

    题目描述 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. 输入 第一行两个正整数n,k,表示数的顶点数和需要选出的点个数. 接下 ...

  8. 【bzoj2427】[HAOI2010]软件安装 Tarjan+树形背包dp

    题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现 ...

  9. 【bzoj4753】[Jsoi2016]最佳团体 分数规划+树形背包dp

    题目描述 JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号.每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY自己看上的.为了 ...

随机推荐

  1. “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ”

    草泥马的adb: “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ” ...

  2. 【HDOJ】3220 Alice’s Cube

    状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...

  3. 动态规划——F 最大矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  4. legoblock秀上限

    很久没有做题了,前天做了一道题结果弱的一逼...搜了解题报告不说...还尼玛秀了上限 题意: 给出宽和高为n和m的一堵墙,手上有长为1,2,3,4高均为1的砖,问形成一个坚固的墙有多少种做法. 坚固的 ...

  5. Thinkphp分页时查询条件保存方法

    web应用中经常要根据用户提交的查询条件进行过滤,再以列表方式显示在浏览器上.如果这种查询是多种条件的组合,并要进行分页显示,则如何在分页导航中保持查询条件,是必须解决的问题. 在Thinkphp中, ...

  6. java 编辑报错 非法字符: \ufeff 解决方案

    用Notepad ++ 调成utf-8 格式 bom 或无bom根据情况 新建类 把代码一句句粘进去 ok

  7. hibernate两表连接查询

    1.两表的关联关系为一对一 2.库存表Stock与商品信息表Product 3.库存表查询商品表里的商品名称,商品编号 库存表字段:    private String id;    private ...

  8. 用canvas把图片变成黑白相片

    <!--这里没有代码--> 原来是把灰度系数分别 乘以 每个像素点的三个像素色(R,G,B)的值,然后得到的三个值加起来,再把得到的值赋值进去给每个R ,G,B. 微软的MSDN上提到的是 ...

  9. ie8下$(document).on('mouseover mouseout','ul li',function(){})的bug

    $(document).on('mouseover mouseout','ul li',function(){ if (event.type == 'mouseover') {           c ...

  10. [PWA] 1. Intro to Service worker

    Service worker stays between our browser and noetwork requests. It can help to fetch data from cache ...