POJ1155 TELE【树形dp(背包)】

来源:互联网 发布:泰达网络登录 编辑:程序博客网 时间:2024/06/06 13:04

POJ1155TELE

Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 4995Accepted: 2741

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 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1

又是一道树上背包,dp[p][i]表示p子树中有i个用户的最大盈利,对每个子树t进行背包转移,
dp[p][i] = max(dp[p][i], dp[p][i-j]+dp[t][j]-cost(p,t))。注意要保证dp[p][i-j]状态存在,可以全部预处理为极小的负数来处理。

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <iostream>using namespace std;const int F = 0x80000000;struct node{    int to, cost;    node(int t, int c):to(t), cost(c){}};int n, m;int pay[3010];vector<node> mp[3010];vector<int> dp[3010];void init(int p){    if(mp[p].empty())    {        dp[p].resize(2, F);        return;    }    int cnt = 1;    for(int i = 0; i < mp[p].size(); i++)    {        init(mp[p][i].to);        cnt += dp[mp[p][i].to].size() - 1;    }    dp[p].resize(cnt, F);}void dfs(int p){    dp[p][0] = 0;    if(mp[p].empty())    {        dp[p][1] = pay[p-n+m];        return;    }    for(vector<node>::iterator it = mp[p].begin(); it != mp[p].end(); it++)    {        dfs(it->to);        for(int i = dp[p].size()-1; i >= 1; i--)            for(int j = 1; j <= min(i, (int)dp[it->to].size()-1); j++)                if(dp[p][i-j] != F)                    dp[p][i] = max(dp[p][i], dp[p][i-j] + dp[it->to][j] - it->cost);    }}int main(){    vector<node> t;    scanf("%d%d", &n, &m);    for(int i = 1; i <= n-m; i++)    {        int k;        scanf("%d", &k);        while(k--)        {            int a, c;            scanf("%d%d", &a, &c);            mp[i].push_back(node(a, c));        }    }    for(int i = 1; i <= m; i++)        scanf("%d", &pay[i]);    init(1);    dfs(1);    for(int i = dp[1].size()-1; i >= 0; i--)        if(dp[1][i] >= 0)        {            printf("%d\n", i);            break;        }    return 0;}
2 0
原创粉丝点击