TELE POJ - 1155 (树形dp)

传送门

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
题意:电视台正在播放节目,每个用户都愿意花费某些金额val[i]来看这个节目,电视台在某两点间的传送是需要花费的,且一条线路只用传输一次,问电视台在不亏损的情况下,最多能让多少个用户看见此节目??

输入的有点难看懂,英语渣渣表示真的看了半天:第一行输入n和m,n表示节点总数,m表示用户总数,则电视台总数为n-m。接下来的n-m行(i~n-m),首先输入一个数k,然后输入2k个数,每两个数(A,C)表示,i到A的传输费用为C。最后一行输入m个数,表示用户愿意花费的金额。。。

很基础的一个树形dp,dp[i][j]表示以i为根节点的子树有j个用户的最大剩余花费,状态转移方程就很明显了:

dp[fa][j] = max(dp[fa][j] , dp[fa][j-k] + dp[son][k] - E[i].w);


#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<queue>
#include<vector>
#include<string.h>
#include<iterator>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MX = 3005;
struct Edge {
    int v, w, nxt;
} E[MX * 2];
int head[MX], tot;
int dp[3005][3005] , val[3005];
int n;
void init() {
    memset(head, -1, sizeof(head));
    tot = 0;
}
void add(int u, int v, int w) {
    E[tot].v = v;
    E[tot].w = w;
    E[tot].nxt = head[u];
    head[u] = tot++;
}
int dfs(int fa){
    if(head[fa] == -1)
        return 1;
    int sum = 0 , cost = 1;
    for(int i = head[fa]; ~i; i = E[i].nxt) {
        int son = E[i].v ;
        sum += dfs(son) ;
        for(int j = sum ; j >= cost ; j --){
            for(int k = 0 ; k <= j ; k ++) {
                dp[fa][j] = max(dp[fa][j] , dp[fa][j-k] + dp[son][k] - E[i].w);
            }
        }
    }
    return sum;
}
int main()
{
    int n , m ;
    while(~scanf("%d%d" , &n , &m)){
        init();
        for(int i = 1 ; i  <= n -  m ; i ++){
            int k;
            scanf("%d" , &k);
            while(k--){
                int u , cost;
                scanf("%d%d" , &u , &cost);
                add(i , u , cost) ;
            }
        }
        memset(dp , 0 , sizeof(dp));
        for(int i = 1 ; i <= n ; i ++){
            for(int j = 1 ; j <= m ; j ++)
                dp[i][j] = -inf;
        }
        for(int i = n - m + 1 ; i <= n ; i ++){
            scanf("%d" , &val[i]) ;
            dp[i][1] = val[i] ;
        }
        dfs(1);
        for(int i = m ; i >= 0 ; i --){
            //cout<<dp[1][i]<<"..."<<endl;
            if(dp[1][i] >= 0){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值