TELE
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4957   Accepted: 2726

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

Source


题意:叶子节点有权值,边有花费,选一些叶子节点用边连起来使总和>0,最多选几个叶子

经典题
f[i][j]表示子树i中选j个叶子的最大价值
dp时处理一下子树规模son[i],体积可以用这个son[i]
叶子节点处理一下
按分给每个子节点的体积分组背包,0可以不考虑,因为0就是没价值
//
// main.cpp
// hdu4003
//
// Created by Candy on 9/23/16.
// Copyright ? 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=3e3+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
struct edge{
int v,w,ne;
}e[N<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int n,m,k,v,w,val[N],son[N];
int f[N][N];
void dp(int u,int fa){
for(int i=;i<=n;i++) f[u][i]=-INF;
if(u>=n-m+){f[u][]=val[u];son[u]=;return;} for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(v==fa) return;
dp(v,u); son[u]+=son[v];
for(int j=son[u];j>=;j--)
for(int k=;k<=son[v];k++){
f[u][j]=max(f[u][j],f[u][j-k]+f[v][k]-w);
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=n-m;i++){
k=read();
while(k--){v=read();w=read();ins(i,v,w);}
}
for(int i=n-m+;i<=n;i++) val[i]=read();
dp(,);
for(int i=n;i>=;i--) if(f[][i]>=){printf("%d",i);break;}
}
 
 
f[i][j]表示子树i中选

最新文章

  1. POJ 2528 Mayor&#39;s posters(线段树/区间更新 离散化)
  2. 用WinDbg调试Windows和驱动程序
  3. C#学习之Linq to Xml
  4. Css3中的变形与动画
  5. 3D MAX在立方体的使用
  6. boa配置文件详解
  7. GL_GL系列 - 总账系统基础(概念)
  8. JSON解析保存在类中
  9. C#_delegate - 异步调用实例 BeginInvoke EndInvoke event
  10. 加载网络映射盘中的assembly失败的处理办法
  11. 13年山东省赛 The number of steps(概率dp水题)
  12. JavaScript声音播放
  13. spring boot整合mybatis方式一
  14. ERROR: invalid byte sequence for encoding &quot;UTF8&quot;: 0x00
  15. Handler机制
  16. Java中类与类的关系
  17. es match、match_phrase、query_string和term的区别
  18. centos(7) 使用yum进行安装lamp环境
  19. 7 款灵巧实用的 CSS3/jQuery 工具
  20. (转)rsync+inotify实时同步

热门文章

  1. WCF的三个名称/命名空间,你是否傻傻分不清楚?
  2. 每天checklist所用到的T-CODE
  3. Snort - 配置文件
  4. ArcGIS Server 标准版和高级版在Web3D的区别
  5. 无插件的大模型浏览器Autodesk Viewer开发培训-武汉-2014年8月28日 9:00 &ndash; 12:00
  6. Autodesk 产品开发培训开始报名-8月26~28-武汉&ndash; Revit, Vault, Autodesk Viewer, Navisworks
  7. 基于XMPP的IOS聊天客户端程序
  8. 三种经典iPhone上网络抓包方法详解
  9. 【代码笔记】iOS-设置textView或者label的行间距方法
  10. Java中定义Map常量,List常量