poj-1155 TELE (树形分组背包)

2024-01-08 14:08
文章标签 分组 背包 poj 树形 tele 1155

本文主要是介绍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 (树形分组背包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/583709

相关文章

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

使用C#如何创建人名或其他物体随机分组

《使用C#如何创建人名或其他物体随机分组》文章描述了一个随机分配人员到多个团队的代码示例,包括将人员列表随机化并根据组数分配到不同组,最后按组号排序显示结果... 目录C#创建人名或其他物体随机分组此示例使用以下代码将人员分配到组代码首先将lstPeople ListBox总结C#创建人名或其他物体随机分组

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

csu(背包的变形题)

题目链接 这是一道背包的变形题目。好题呀 题意:给n个怪物,m个人,每个人的魔法消耗和魔法伤害不同,求打死所有怪物所需的魔法 #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>//#include<u>#include<map

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm