现在的位置: 首页 > 综合 > 正文

Poj 1155 TELE

2018年01月15日 ⁄ 综合 ⁄ 共 2547字 ⁄ 字号 评论关闭
TELE
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2983   Accepted: 1525

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

树形DP,树形分组背包

#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

#define maxn 3100
#define Max_(a,b) (a>b?a:b)

vector<int> num[maxn];

int N,M;
int Val[maxn],S[maxn];              //将边的值转化为结点的值,Val保存每个结点的值,S保存每个结点的最大用户数
int d[maxn][maxn];

void DFS(int x)
{
	int i,j,k,max=0;
	if(num[x].size()==0)
	{
		d[x][1]=Val[x];
		S[x]=1;
		return ;
	}

	for(k=0;k<num[x].size();k++)
	{
		DFS(num[x][k]);
		max+=S[num[x][k]];
	}
	S[x]=max;
	
	int hash[maxn];
	memset(hash,0,(max+5)*sizeof(hash[0]));
	hash[0]=1;	d[0][0]=0;
	for(k=0;k<num[x].size();k++)
	{
		for(j=max;j>=1;j--)
		{
			for(i=1;i<=S[num[x][k]];i++)
			{
				if(j>=i && hash[j-i])
				{
					if(hash[j]==0)
					{
						hash[j]=1;
						d[x][j]=d[num[x][k]][i]+d[x][j-i];
					}
					else
					{
						d[x][j]=Max_(d[x][j],d[x][j-i]+d[num[x][k]][i]);
					}
				}
			}
		}
	}
	for(i=1;i<=S[x];i++)
		d[x][i]+=Val[x];
}

int main()
{
	int i,j,k,A,C;

	while(scanf("%d%d",&N,&M)==2)
	{
		for(i=1;i<=N-M;i++)
		{
			num[i].clear();
			scanf("%d",&k);
			while(k--)
			{
				scanf("%d%d",&A,&C);
				num[i].push_back(A);
				Val[A]=-C;
			}
		}
		while(i<=N)
		{
			scanf("%d",&C);
			Val[i++]+=C;
		}

		DFS(1);
		int max=0;
		for(i=S[1];i>=0;i--)
		{
			if(d[1][i]>=0)
			{
				max=i;
				break;
			}
		}
		printf("%d\n",max);

	}

	return 0;
}

抱歉!评论已关闭.