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

相关文章

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的stream流分组示例详解

《Java中的stream流分组示例详解》Java8StreamAPI以函数式风格处理集合数据,支持分组、统计等操作,可按单/多字段分组,使用String、Map.Entry或Java16record... 目录什么是stream流1、根据某个字段分组2、按多个字段分组(组合分组)1、方法一:使用 Stri

SpringBoot利用树形结构优化查询速度

《SpringBoot利用树形结构优化查询速度》这篇文章主要为大家详细介绍了SpringBoot利用树形结构优化查询速度,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一个真实的性能灾难传统方案为什么这么慢N+1查询灾难性能测试数据对比核心解决方案:一次查询 + O(n)算法解决

SpringBoot结合Knife4j进行API分组授权管理配置详解

《SpringBoot结合Knife4j进行API分组授权管理配置详解》在现代的微服务架构中,API文档和授权管理是不可或缺的一部分,本文将介绍如何在SpringBoot应用中集成Knife4j,并进... 目录环境准备配置 Swagger配置 Swagger OpenAPI自定义 Swagger UI 底

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Java如何根据文件名前缀自动分组图片文件

《Java如何根据文件名前缀自动分组图片文件》一大堆文件(比如图片)堆在一个目录下,它们的命名规则遵循一定的格式,混在一起很难管理,所以本文小编就和大家介绍一下如何使用Java根据文件名前缀自动分组图... 目录需求背景分析思路实现代码输出结果知识扩展需求一大堆文件(比如图片)堆在一个目录下,它们的命名规

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

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

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

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

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

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

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