树形DP+背包(poj1155泛化分组背包)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3675 | Accepted: 1936 |
Description
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 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
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
题意:一个树形网络,编号为1的是广播站,叶子节点为广播接收者,要想使用必须付出费用,中间的其他点是中继站,信号到达每个中继站都会有一定的消费;问在保证广播站收益不亏本的情况下使用者最多是多少? 分析:dp[i][j]表示第i个点下面有j个使用用户的收益;状态转移方程: dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-edge[i].w); 其中u是v的父节点,j枚举u节点的所有子叶点数,k枚举v节点的所有子叶点数,edge[i].w表示u节点下的使用用户不是0的话要减去的成本 dp全部初始化为-inf,dp[i][0]赋为0;
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define eps 1e-10
#define M 3333
#define inf 99999999
#include"algorithm"
#define g 9.8
#define PI acos(-1.0)
using namespace std;
int dp[M][M];
struct node
{
int v,w;
node(int vv,int ww)
{
v=vv;
w=ww;
}
};
vector<node>edge[M];
int cost[M],sum[M],use[M],m,n;
void dfs(int u)
{
use[u]=1;
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(!use[v])
{
dfs(v);
sum[u]+=sum[v];
for(int j=sum[u];j>=0;j--)
{
for(int k=1;k<=sum[v];k++)
{
if(j>=k)
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-edge[u][i].w);
}
}
}
}
if(u>n-m)
{
sum[u]=1;
dp[u][1]=cost[u];
}
}
int main()
{
int i,j,k,c;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++)
edge[i].clear();
for(i=1;i<=n-m;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&j,&c);
edge[i].push_back(node(j,c));
edge[j].push_back(node(i,c));
}
}
for(i=n-m+1;i<=n;i++)
scanf("%d",&cost[i]);
for(i=1;i<=n;i++)
{
dp[i][0]=0;
for(j=1;j<=m;j++)
dp[i][j]=-inf;
}
memset(sum,0,sizeof(sum));
memset(use,0,sizeof(use));
dfs(1);
//for(i=1;i<=m;i++)
//printf("%d ",dp[1][i]);
for(i=m;i>=1;i--)
{
if(dp[1][i]>=0)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
树形DP+背包(poj1155泛化分组背包)的更多相关文章
- HDU 1561 The more, The Better【树形DP/有依赖的分组背包】
ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...
- HDU 1011 Starship Troopers【树形DP/有依赖的01背包】
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...
- Regionals 2014 >> Asia - Taichung 7003 - A Balance Game on Trees 树形DP + 二维费用背包
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- HDU - 6125: Free from square (状压DP+分组背包)
problem:给定N,K.表示你有数1到N,让你最多选择K个数,问有多少种方案,使得选择的数的乘积无平方因子数.N,K<500: solution:显然可以状压DP做,但是500以内的素数还是 ...
- hdu3033 I love sneakers! 分组背包变形(详解)
这个题很怪,一开始没仔细读题,写了个简单的分组背包交上去,果不其然WA. 题目分析: 分组背包问题是这样描述的:有K组物品,每组 i 个,费用分别为Ci ,价值为Vi,每组物品是互斥的,只能取一个或者 ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 【DP_树形DP专题】题单总结
转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...
- 树形dp 入门
今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...
- 「算法笔记」树形 DP
一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...
随机推荐
- JQuery _ 定时器(jQuery Timers) 学习
jQuery Timers插件地址: http://plugins.jquery.com/project/timers JQuery Timers应用知识 提供了三个函式 1. everyTime(时 ...
- 转载:pyqt线程间通过 信号/槽 通信
转自:http://blog.sina.com.cn/s/blog_613d5bb701016qzv.html 信号(singal)与槽(slot)用于对象相互通信,信号:当某个对象的某个事件发生时, ...
- CentOS 6.5 下利用命令行截图及设置快捷键截图
CentOS 6.5 下利用命令行截图及设置快捷键截图 1.利用命令模式 捕获整个屏幕 : $ gnome-screenshot 截完屏之后我们可以设置自定义图片存储位置,如图: 捕获当前终端Term ...
- Unity中坐标系转换方法
前言 本篇文章主要是参考<Unity API 解析>---陈泉宏. 这是本人在学校图书馆找到一本书,主要介绍的就是常用的类,比较实用,没有冗余的地方.在此推荐一下这本书! 一.Screen ...
- 移动端 touch
原文链接:http://caibaojian.com/mobile-touch-event.html 本文主要介绍 TouchEvent 相关的一些对象与属性如 Touch, TouchList, t ...
- hbase学习 rowKey的设计-4
访问hbase table中的行,只有三种方式: 1 通过单个row key访问 2 通过row key的range 3 全表扫描 Hadoop Sequence File 文中可能涉及到的API: ...
- dedecms的arclist循环中判断第一个li添加css,否则不加
dedecms的arclist循环中,判断如果是第一个li,则添加固定的css,否则不加 写法如下: {dede:arclist row=4 flag='p'} <li [field:glo ...
- 从css样式表中抽取元素尺寸
jS从样式表取值的函数.IE中以currentStyle,firefox中defaultView来获取 DOM.style仅仅能读到写在html中的样式值 获取样式值的函数 function retu ...
- swif开发之--协议的使用
以前在oc构建的项目中,如果这个页面需要构建一些指定的页面,一般我会重新创建个集成与UIView的类,然后同时创建XXX.xib文件,然后直接在上面拖拽控件,非常快速,当然也可以手动布局!个人更喜欢可 ...
- 查看磁盘读写:iostat
iostat命令用来查看磁盘IO的读写情况,用法如下: [root@localhost ~]$ yum install -y sysstat # 安装iostat命令 [root@localhost ...