POJ1155TELE[树形背包]

Posted Candy?

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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+5,INF=1e9;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}
struct edge{
    int v,w,ne;
}e[N<<1];
int h[N],cnt=0;
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=1;i<=n;i++) f[u][i]=-INF;
    if(u>=n-m+1){f[u][1]=val[u];son[u]=1;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>=1;j--)
            for(int k=1;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=1;i<=n-m;i++){
        k=read();
        while(k--){v=read();w=read();ins(i,v,w);}
    }
    for(int i=n-m+1;i<=n;i++) val[i]=read();
    dp(1,0);
    for(int i=n;i>=1;i--) if(f[1][i]>=0){printf("%d",i);break;}
}

 

 
 
f[i][j]表示子树i中选

以上是关于POJ1155TELE[树形背包]的主要内容,如果未能解决你的问题,请参考以下文章

POJ1155 TELE(树形DP)

POJ - 1155 TELE 树形dp

poj 1155 TELE

TELE poj1155 题解

POJ 1155 树形背包

[POJ1155]TELE