本文主要是介绍poj-1155 TELE (树形分组背包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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[u][j]是以u为结点,子树中支持j个客户的最大收益,(这个收益可以是一个负数)。
如果该结点是叶子节点,dp[u][1] = val[u];
如果该结点是中转站,dp[u][j] = dp[u][j-k]+dp[v][k]-Weight_u_v.
其中u,代表目前结点编号,v代表儿子结点的编号,j和k都代表客户个数。
通过对每个中转站做背包,就能得到每个节点支持j个客户的最大收益。
最后再遍历dp[1][i],找到最大的i满足dp[1][i]>=0就可以了
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<queue>using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 3005;struct Edge
{int to,nxt;int w;
} edges[maxn<<2];
int head[maxn<<2];
int tot;void addEdge(int u, int v, long long w)
{edges[tot] = Edge{v,head[u],w};head[u] = tot++;
}int dp[maxn][maxn]; //第u个结点,有k个子结点的最大收益。
int sum[maxn];
int val[maxn];
int n, m;void dfs(int u, int fa)
{if(u>n-m){sum[u] = 1;dp[u][1] = val[u];return ;}for(int i = head[u]; ~i; i = edges[i].nxt){int v = edges[i].to;int w = edges[i].w;if(v == fa)continue;dfs(v,u);sum[u]+=sum[v];for(int j = sum[u]; j >= 0; j--){for(int k = 1; k <= sum[v]; k++){if(j-k>=0)dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[v][k]-w);}}}
}int main()
{int k, a;int c;scanf("%d%d", &n, &m);tot = 1;memset(head,-1,sizeof(head));memset(dp,-inf,sizeof(dp));
// cout << dp[1][1]<<endl;for(int i = 1; i <= n-m; i++){scanf("%d", &k);for(int j = 1; j <= k; j++){scanf("%d%d", &a, &c);addEdge(i,a,c);}dp[i][0] = 0;}for(int i = n-m+1; i <= n; i++){scanf("%d", &val[i]);dp[i][0] = 0;}dfs(1,-1);for(int i = sum[1]; i >=0; i--){if(dp[1][i]>=0){printf("%d\n", i);break;}}return 0;
}
这篇关于poj-1155 TELE (树形分组背包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!