POJ1155 TELE 树形DP

被这道题目坑了很久,一直TLE。最初以为是因为自己偷懒用了vector容器的缘故,后来把vector改成了自己写的结构数组,但发现还是TLE,于是在网上搜了一下代码,

发现通过增加一个数组,记录每一个节点最多能够联通的叶子节点个数,可以有效的减少无用的循环次数。最终ac时间是110ms(。。。差别果然大)。看来喝太多水了。。。。

 做法还是和之前的树形DP背包问题相似,只是把父亲节点到子节点的消耗作为子节点的负收益。最终只要求出联通叶子节点个数最多收益非负的情况,输出个数即可。

TELE

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 2315

 

Accepted: 1136

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
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>

using namespace  std;

#define INF 0xFFFFFFF

int dp[3010][3010];   //[节点编号][传送到的用户数量] 
int n,m;
int num[3010];       //该数组用于记录以i为根节点,能够通向的叶子节点个数
                     //使用该数组能够有效减少循环次数 
struct nodes{
	int next;
	int child;
	int   c;
}node[3010]; 
 
int max(int a,int b)
{
	return a>b?a:b;
}

void init()
{
	int a,b,co;
	for(int i=1;i<=n;i++)
	{
		node[i].next=0;
		node[i].child=0;
		node[i].c=0;
		num[i]=0;
		for(int j=1;j<=n;j++)
			dp[i][j]=-INF;
	}
	node[1].c=0;
	for(int i=1;i<=n-m;i++)
	{
		scanf("%d",&co);
		for(int j=1;j<=co;j++)
		{
			scanf("%d%d",&a,&b);
			node[a].next=node[i].child;
			node[i].child=a;
			node[a].c=0-b;   //将父亲节点到子节点的消耗,看做是子节点的负收益 
		}	
	}
	for(int i=n-m+1;i<=n;i++)
	{
		scanf("%d",&a);
		node[i].c+=a; 
	} 	
}

void dfs(int root)
{
	int temp[3010];
	for(int i=0;i<=n;i++)
		temp[i]=-INF;
	if(root>=n-m+1)
	{
		dp[root][0]=0;
		num[root]=1;
		dp[root][1]=node[root].c;
		return ;
	} 
	dp[root][0]=temp[0]=0;
	int child=node[root].child;
	while(child!=0)
	{
		int cc=child;
		dfs(cc);
		num[root] += num[cc] ;  
		for(int j=num[root];j>=0;j--)    //此处是使用nun[]数组 
		{
			for(int k=0;k<=num[cc];k++)  //此处使用num[]数组 。这两个地方使用num[]后可以减少大量的无用循环
			{                            //之前没有加一直TLE 
				if(j-k<0)
					break;
				if(dp[cc][k]!=-INF && temp[j-k]!=-INF)
					temp[j]=max(temp[j],temp[j-k]+dp[cc][k]);
			}
		}
		child=node[child].next;
	}
	for(int i=1;i<=m;i++)
	{
		if(temp[i]!=-INF)
			dp[root][i]=temp[i]+node[root].c;
	}	
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		dfs(1);
		for(int i=m;i>=0;i--)
		{
			if(dp[1][i]>=0)
			{
				cout<<i<<endl;
				break;
			}
		} 	
	}
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值