[luoguP1273] 有线电视网(DP)

传送门

 

f[i][j]表示节点i选j个用户的最大收益

 

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 3001
#define max(x, y) ((x) > (y) ? (x) : (y))

int n, m, cnt;
int head[N], to[N], next[N], val[N], f[N][N], size[N];
//f[i][j]表示第i个节点选择j个用户的最优解 

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

inline void add(int x, int y, int z)
{
	to[cnt] = y;
	val[cnt] = z;
	next[cnt] = head[x];
	head[x] = cnt++;
}

inline void dfs(int u)
{
	int i, j, k, v;
	size[u] = 1;
	for(i = head[u]; i ^ -1; i = next[i])
	{
		v = to[i];
		dfs(v);
		size[u] += size[v];
		for(j = size[u]; j >= 1; j--)
			for(k = 1; k <= j; k++)
				f[u][j] = max(f[u][j], f[u][j - k] + f[v][k] - val[i]);
	}
}

int main()
{
	int i, j, k, x, y;
	n = read();
	m = read();
	memset(head, -1, sizeof(head));
	for(i = 1; i <= n - m; i++)
	{
		k = read();
		for(j = 1; j <= k; j++)
		{
			x = read();
			y = read();
			add(i, x, y);
		} 
	}
	for(i = 1; i <= n; i++)
		for(j = 1; j <= n; j++)
			f[i][j] = -10000;
	for(i = n - m + 1; i <= n; i++) f[i][1] = read();
	dfs(1);
	for(i = m; i >= 0; i--)
		if(f[1][i] >= 0)
		{
			printf("%d\n", i);
			return 0;
		}
	return 0;
}

  

posted @ 2017-08-11 09:40  zht467  阅读(145)  评论(0编辑  收藏  举报