[poj1155] TELE 树形DP 01背包

TELE
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3445 Accepted: 1781

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
【题意】给定一棵树,结点1表示电视台,叶子节点表示客户,中间结点表示中转站。每个客户结点有收益,每条边有代价,到达客户节点得到该节点的收益,经过一条边付出其代价。要求总收益-总代价>0,选择一些边,要求达到最多的客户节点。
【分析】显然是树形DP。考虑结点i和它的子树,用dp[i][j]表示选择j个客户的(总收益-总代价)max。枚举增加的客户数量k,对于i的每个儿子结点s,收益为dp[i][j]+dp[s][k],代价为边权。得到状态转移方程dp[i][j+k] = max(dp[i][j+k], dp[i][j]+dp[s][k]-e.dist)。
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 2050;
const int inf = 1e9+7;
void _max(int& x, int y) {
	if(y > x) x = y;
}
struct edge {
	int to, dist;
};
vector<edge> G[maxn];
int n, m;
int e[maxn][maxn], num[maxn], t[maxn];

void solve(int u) {
	if(u >= n-m) return;
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i].to, w = G[u][i].dist;
		solve(v);
	    for(int k = 0; k <= num[u]; k++) t[k] = e[u][k];
        for(int j = 0; j <= num[u]; j++) for(int k = 1; k <= num[v]; k++) _max(e[u][j+k], t[j]+e[v][k]-w);
        num[u] += num[v];
    }
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) e[i][j] = -inf;
	for(int i = 0; i < n-m; i++) {
		int nn;
		scanf("%d", &nn);
		while(nn--) {
			int v, w;
			scanf("%d%d", &v, &w);
			G[i].push_back((edge){v, w});
		}
	}
	for(int i = n-m; i < n; i++) {
		scanf("%d", &e[i][1]);
		num[i] = 1;
	}
	solve(0);
	for(int i = m; i >= 0; i--) if(e[0][i]) {
		printf("%d\n", i);
		break;
	}
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值