Poj-1155 TELE(树形背包)

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


题意:有个通讯公司,有一个发射台m个客户和n-m-1个中转站,所有的线路呈树形结构,m个客户位于叶子节点,每条边都有一定费用消耗,每个客户愿意为收看直播付一定费用,问在不亏损的情况下最多为多少客户服务。


分析:我的做法是n*m的复杂度,求出树的DFS序后,f[i][j]表示由序列中的第i个点开始服务j个客户的最大获利,然后就是类似背包的n*m转移。


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define eps 1e-9
#define N 3005
#define MOD 998244353
using namespace std;
typedef long long ll;
int n,m,u,time,cost,k,value[N],num[N],last[N],f[N][N];
vector G[N];
void dfs(int u)
{
    num[++time] = u;
    for(int i = 0;i < G[u].size();i++)
    {
        int v = G[u][i];
        dfs(v);
    }
    last[u] = time;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n-m;i++)
    {
        scanf("%d",&k);
        for(int j = 1;j <= k;j++)
        {
            scanf("%d%d",&u,&cost);
            value[u] -= cost;
            G[i].push_back(u);
        }
    }
    for(int j = n-m+1;j <= n;j++)
    {
        scanf("%d",&cost);
        value[j] += cost;
    }
    dfs(1);
    for(int i = 1;i <= m;i++) f[n+1][i] = -INF;
    for(int i = n;i;i--)
     for(int j = 1;j <= m;j++)
     {
         f[i][j] = f[last[num[i]]+1][j];
         if(num[i] > n - m) f[i][j] = max(f[i][j],f[i+1][j-1] + value[num[i]]);
         else f[i][j] = max(f[i][j],f[i+1][j] + value[num[i]]);
     }
    for(int i = m;i >= 0;i--)
     if(f[1][i] >= 0)
     {
        printf("%d\n",i);
        return 0;
     }
}


你可能感兴趣的:(ACM,DP动态规划)