TELE
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3675   Accepted: 1936

Description

A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match, the leaves of the tree are the potential users and other
vertices in the tree are relays (transmitters). 

The price of transmission of a signal from one transmitter to another or to the user is given. A price of the entire broadcast is the sum of prices of all individual signal transmissions. 

Every user is ready to pay a certain amount of money to watch the match and the TV-network then decides whether or not to provide the user with the signal. 

Write a program that will find the maximal number of users able to watch the match so that the TV-network's doesn't lose money from broadcasting the match.

Input

The first line of the input file contains two integers N and M, 2 <= N <= 3000, 1 <= M <= N-1, the number of vertices in the tree and the number of potential users. 

The root of the tree is marked with the number 1, while other transmitters are numbered 2 to N-M and potential users are numbered N-M+1 to N. 

The following N-M lines contain data about the transmitters in the following form: 

K A1 C1 A2 C2 ... AK CK 

Means that a transmitter transmits the signal to K transmitters or users, every one of them described by the pair of numbers A and C, the transmitter or user's number and the cost of transmitting the signal to them. 

The last line contains the data about users, containing M integers representing respectively the price every one of them is willing to pay to watch the match.

Output

The first and the only line of the output file should contain the maximal number of users described in the above text.

Sample Input

9 6
3 2 2 3 2 9 3
2 4 2 5 2
3 6 2 7 2 8 2
4 3 3 3 1 1

Sample Output

5
题意:一个树形网络,编号为1的是广播站,叶子节点为广播接收者,要想使用必须付出费用,中间的其他点是中继站,信号到达每个中继站都会有一定的消费;问在保证广播站收益不亏本的情况下使用者最多是多少?                                                   分析:dp[i][j]表示第i个点下面有j个使用用户的收益;状态转移方程:                                                       dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-edge[i].w);                                                             其中u是v的父节点,j枚举u节点的所有子叶点数,k枚举v节点的所有子叶点数,edge[i].w表示u节点下的使用用户不是0的话要减去的成本 dp全部初始化为-inf,dp[i][0]赋为0;
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define eps 1e-10
#define M 3333
#define inf 99999999
#include"algorithm"
#define g 9.8
#define PI acos(-1.0)
using namespace std;
int dp[M][M];
struct node
{
int v,w;
node(int vv,int ww)
{
v=vv;
w=ww;
}
};
vector<node>edge[M];
int cost[M],sum[M],use[M],m,n;
void dfs(int u)
{
use[u]=1;
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(!use[v])
{
dfs(v);
sum[u]+=sum[v];
for(int j=sum[u];j>=0;j--)
{
for(int k=1;k<=sum[v];k++)
{
if(j>=k)
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-edge[u][i].w);
}
}
}
}
if(u>n-m)
{
sum[u]=1;
dp[u][1]=cost[u];
}
}
int main()
{
int i,j,k,c;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++)
edge[i].clear();
for(i=1;i<=n-m;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&j,&c);
edge[i].push_back(node(j,c));
edge[j].push_back(node(i,c));
}
}
for(i=n-m+1;i<=n;i++)
scanf("%d",&cost[i]);
for(i=1;i<=n;i++)
{
dp[i][0]=0;
for(j=1;j<=m;j++)
dp[i][j]=-inf;
}
memset(sum,0,sizeof(sum));
memset(use,0,sizeof(use));
dfs(1);
//for(i=1;i<=m;i++)
//printf("%d ",dp[1][i]);
for(i=m;i>=1;i--)
{
if(dp[1][i]>=0)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}

树形DP+背包(poj1155泛化分组背包)的更多相关文章

  1. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

  2. HDU 1011 Starship Troopers【树形DP/有依赖的01背包】

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...

  3. Regionals 2014 >> Asia - Taichung 7003 - A Balance Game on Trees 树形DP + 二维费用背包

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  4. HDU - 6125: Free from square (状压DP+分组背包)

    problem:给定N,K.表示你有数1到N,让你最多选择K个数,问有多少种方案,使得选择的数的乘积无平方因子数.N,K<500: solution:显然可以状压DP做,但是500以内的素数还是 ...

  5. hdu3033 I love sneakers! 分组背包变形(详解)

    这个题很怪,一开始没仔细读题,写了个简单的分组背包交上去,果不其然WA. 题目分析: 分组背包问题是这样描述的:有K组物品,每组 i 个,费用分别为Ci ,价值为Vi,每组物品是互斥的,只能取一个或者 ...

  6. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  7. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  8. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  9. 「算法笔记」树形 DP

    一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...

随机推荐

  1. 关于Unity中使用刚体制作简单跑酷案例

    一.步骤 1.创建一个Canvas 2.对Canvas进行初始化,记得把Game视图的分辨率调成和Canvas里面设置的一样的分辨率960X640 3.创建一个Image的UI节点作为Canvas的子 ...

  2. HttpClient学习之 客户端HTTP编程入门

    说明 本文存在的原因,是想深入的学习下HttpClient.对应的网址是: http://hc.apache.org/httpcomponents-client-4.5.x/primer.html h ...

  3. 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果(转)

    转载:http://blog.csdn.net/xiaanming/article/details/10163203 了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从 ...

  4. LabVIEW中数组的自动索引

    我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...

  5. 【Java集合的详细研究7】Set和List 的关系与区别

    两个接口都是继承自Collection. List (inteface) 次序是List 的最重要特点,它确保维护元素特定的顺序. --ArrayList 允许对元素快速随机访问. --LinkedL ...

  6. 【R】shiny界面

    http://www.rstudio.com/shiny http://yanping.me/shiny-tutorial/#welcome

  7. 一个牛人给Java初学者的建议

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:618528494  我们一起学Java! 给初学者之一:浅谈Java及应用学java ...

  8. eclipse (ADT) svn插件 过滤上传的 文件 文件夹 一劳永逸

    其实很简单哈,过滤的有三种类型,1.文件.2.文件夹.3.android的target 在ADT中 window->preferences-> 会打开如下界面 ignore就是忽视的意思 ...

  9. linux数据盘分区以及格式化

    首先切换到root账户下  sudo -i 手动格式化 请根据以下步骤对数据盘进行分区以及格式化,并挂载分区使数据盘可用. 注:执行以下命令时,请注意修改数据盘符,可以使用”fdisk -l”查看盘符 ...

  10. c语言常用数据类型转换整理

    你要发送原始数据流 还是 格式化输出? 如果是格式化 按原子说的 ,用sprintf / printf; 如果发送原始内存数据流, 可按下面发送, 发送 #define BYTE0(pointer) ...