【树形dp】TELE
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5376 | Accepted: 2973 |
Description
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
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=8001;
const int INF=999999;
int N,M;
int Node[MAXN],Root[MAXN],Next[MAXN],Cost[MAXN];
int cnt;
int val[MAXN];
int tel[MAXN];
int dp[3001][3001]; void addedge(int u,int v,int w){
++cnt;
Node[cnt]=v; Cost[cnt]=w;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
}
void dfs(int x){
if(x>N-M){
tel[x]=1;
dp[x][1]=val[x];
dp[x][0]=0;
return ;
}
for(int k=Root[x];k;k=Next[k]){
int son=Node[k];
dfs(son);
}
int tmp=0;dp[x][0]=0;
for(int k=Root[x];k;k=Next[k]){
int son=Node[k];
tmp+=tel[son];
for(int t=tmp;t>=1;t--){
for(int p=1;p<=tel[son];p++){
if(p>t) break;
dp[x][t]=max(dp[x][t],dp[son][p]+dp[x][t-p]-Cost[k]);
}
}
}
tel[x]=tmp;
return ;
} int main(){
N=read(),M=read();
for(int i=0;i<=N;i++)
for(int j=0;j<=M;j++) dp[i][j]=-INF;
for(int i=1;i<=N-M;i++){
int k=read();
while(k--){
int v=read(),w=read();
addedge(i,v,w);
}
}
for(int i=1;i<=M;i++) val[i+N-M]=read();
dfs(1); int ans=M;
while(dp[1][ans]<0) ans--;
printf("%d\n",ans);
}
【树形dp】TELE的更多相关文章
- POJ 1155 TELE 背包型树形DP 经典题
由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- 【转】【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)
1.poj 115 TELE 题意:一个树型网络上有n个结点,1~n-m为信号传送器,n-m+1~n为观众,当信号传送给观众后,观众会付费观看,每铺设一条道路需要一定费用.现在求以1为根,使得收到观众 ...
- 基础树形DP小结
HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
随机推荐
- bzoj 1965 数学
首先我们可以发现每张牌的对应关系,假设序号为x的牌,经过一次洗牌后的位置为: 2*x x<=n/2 2*(x-n/2)-1 x>n/2 那么我们可以将下面的式子化简,变成2*x-n ...
- 详解JS中Number()、parseInt()和parseFloat()的区别
三者的作用: Number(): 可以用于任何数据类型转换成数值: parseInt().parseFloat(): 专门用于把字符串转换成数值: 一.Number( ): (1)如果是Boolean ...
- JSOI2018简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. 有幸拜读到贵省的题目,题的质量还不错,而且相比zjoi可做多了,简单发一下题解吧. 还有就是,怎么markdown在博客园上的代码这么丑啊 「 ...
- TypeError: expected string or buffer的解决方法
错误种类:TypeError: expected string or buffer 具体错误解释:这是因为返回的变量不是字符类型,而导致此错误 具体解决方法:在具体程序段前加if判断语句,判断程序返回 ...
- Python3 面向对象编程
小案例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Bert import sys class Role(object): n=&qu ...
- Django 1.10中文文档-第一个应用Part4-表单和通用视图
本教程接Part3开始.继续网页投票应用程序,并将重点介绍简单的表单处理和精简代码. 一个简单表单 更新一下在上一个教程中编写的投票详细页面的模板polls/detail.html,让它包含一个HTM ...
- linux===Ubuntu 上安装 Node.js
https://www.cnblogs.com/andfly/p/6681487.html
- selenium WebElement 的属性和方法 属性
tag_name 标签名,例如 'a'表示<a>元素get_attribute(name) 该元素name 属性的值text 该元素内的文本,例如<span>hello< ...
- Oracle 内存顾问
--查看内存相关参数SYS@ test10g> col name for a30SYS@ test10g> col value for a20SYS@ test10g> select ...
- 【bzoj3786】星系探索
ETT模版题. 真正的Eular-Tour-Tree维护的是树的欧拉序. 由于各种原因,没人知道怎么维护欧拉序,所以我写的是个假的,维护dfs序的. 本质还是用Splay维护序列. 然后因为我常数太差 ...