您的位置:首页 > 其它

解题报告:POJ_1155 TELE 树型DP(树上01背包)

2016-12-30 15:05 337 查看
TELE

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5117 Accepted: 2810
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

Source

Croatia OI 2002 Final Exam - Second Day
[Submit]   [Go Back]   [Status]  
[Discuss]

题意:
给一棵树,每条边和每个叶子都有一个权值,让你选择一个包含叶子数最多的子树,满足所有叶子的权值和减去所有边的权值和的结果为非负值

思路:
很有趣的题目,对树上的每个节点的每棵子树都分别做一次01背包,叶子节点初始化dp[i][1] = w[i]
dp[i][j] 表示 节点i的子树中选择j个叶子的最大所求值
cost[t]表示节点t到父亲的边的权值
w[i]表示叶子节点i的权值
dp[i][j] = max( dp[i][j] , dp[i][j-k] + dp[t][k] - cost[t])
注意在一次背包中较前的修改可能会影响到后面的决策,这时常见的解决方法就是先用临时的数组保存之前的值,其实也可以自己设计决策的顺序来避开上诉情况。

代码:
#include<vector>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

int dp[3005][3005];
int tmp[3005];
int cost[3005];
int w[3005];
vector<int>G[3005];

int n,m;
void dfs(int x){
if(!G[x].size())dp[x][1]=w[x];
for(int i=0;i<G[x].size();i++){
int &t = G[x][i];
dfs(t);
for(int i=0;i<=n;i++){
tmp[i]=dp[x][i];
}for(int j=1;j<=n&&dp[t][j]!=dp[0][0];j++){
for(int k=n;k>=j;k--)if(tmp[k-j]!=dp[0][0]){
dp[x][k]=max(dp[x][k],tmp[k-j]+dp[t][j]-cost[t]);
}
}
}
}

int main()
{
while(scanf("%d%d",&n,&m)==2){
memset(dp,0xaf,sizeof(dp));
for(int i=1;i<=n;i++){
w[i]=cost[i]=dp[i][0]=0;
G[i].clear();
}for(int i=1,k,t,c;i<=n-m;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&t);
scanf("%d",&cost[t]);
G[i].push_back(t);
}
}for(int i=n-m+1;i<=n;i++){
scanf("%d",&w[i]);
}dfs(1);
int ans = 0;
for(int i=n;i;i--){
if(dp[1][i]>=0){
ans = i;
break;
}
}printf("%d\n",ans);
}return 0;
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dp[i][j]!=dp[0][0]){
printf("%d %d -----> %d\n",i,j,dp[i][j]);
}
}
}*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: