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. chrome 常用快捷键(可以摆脱鼠标哦)

      Ctrl+N                                打开新窗口. Ctrl+T                                打开新标签页. Ctrl+Sh ...

  2. C++异常抛出与捕获及处理

    一.异常 迄今为止,我们处理程序中的错误一般都是用if语句测试某个表达式,然后处理错误的特定义代码. C++异常机制使用了三个新的关键字  (SEH(结构化异常处理)) try    ──标识可能出现 ...

  3. wampserver下配置虚拟主机 实现多站点支持

    wampserver下配置虚拟主机 实现多站点支持 wampserver是由德国人开发,在windows环境下实现apache+php+mysql的一款集成环境软件,wampserver自带SQLit ...

  4. 在windows下编译ffmpeg

    编译ffmpeg,我在网上找了很多相关的方法,但最后都没编译成功. 所以下面就记录下自己的编译方法吧,留着以后编译的时候做参考. 1.首先,下载编译工具MinGW+Msys,搭建编译环境.工具下载地址 ...

  5. 直接拿来用!最火的Android开源项目(转)

    摘要:对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,CSDN特整理了GitHub上最受欢迎的Android及iOS开源项目,本文详细介绍了20 ...

  6. AsyncTask执行顺序

    这几天,遇见个奇葩问题,记录一下. 在用AsyncTask的时候,new 出来的AsyncTask总是等了很久才执行到. 于是乎,想到了是不是前面已经有好几个AsyncTask的实例了,是不是线程优先 ...

  7. Kubernetes(二)架构及资源关系简单总结

    Kubernetes架构 先引用一下官方的架构图: 对于本文来说,我觉得这张图有点复杂了,但是我又懒得自己画了,就用这张吧.Kubernetes是一个集群,和传统的集群相似,它也是有一个主节点和若干个 ...

  8. oracle中如何把结果集插入临时表中

    比如临时表叫temp,你要查询的语句为select * from 表名 where id=1. 1.建表temp 2.插入语句: ; commit;  

  9. <UIKit>关于剪贴板共享数据

     http://blog.sina.com.cn/s/blog_45e2b66c010102h9.html 上面这篇文章将剪贴板的使用方法基本上已经讲清楚了,参考这篇文章,再加上一个使用剪贴板共享数据 ...

  10. Unity带参数的协程

    两种方法都可以传递参数,代码如下: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { v ...