POJ1155TELE[树形背包]

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中选
上一篇:EF分页中的陷阱


下一篇:【HBase学习】Apache HBase 参考手册 中文版