【树形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 ...
随机推荐
- html+js+node实现五子棋线上对战,五子棋最简易算法
首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...
- 1.0 docker介绍
简介: 一种虚拟化的方案 将应用程序自动部署到容器 特点: 轻量 环境的一直性 提高开发生命周期 使用面向服务的架构 场景: 开发.测试.部署 创建隔离的运行环境 集群测试环境 云计算应用 ...
- linux 3389连接工具Rdesktop
简单使用 工作机换成战斗机了,改用ubuntu,原来的windows7上东西笔记多,还不想重装.用rdesktop来远程连接windows: sudo apt-get install rdesktop ...
- Java面向对象的三个特征与含义
封装 1.英文为 encapsulation,实现信息隐藏: 2.把同一类事物的特性归纳到一个类中(属性和行为),隐藏对象的内部实现: 继承 1.英文为 inheritance: 2.继承的过程,是从 ...
- Revison
- thread线程栈size及局部变量最大可分配size【转】
转自:http://blog.csdn.net/sunny04/article/details/46805261 版权声明:本文为博主原创文章,未经博主允许不得转载. 进程是操作系统的最小资源管理单元 ...
- bzoj 1179 Atm
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 题解: 一道比较综合的图论题 直接讲正解: 如果这个图G中存在某个强连通分量,那么这 ...
- 一个真正的客户端非阻塞的 connect
前言 - 一个简短开场白 winds 的 select 和 linux 的 select 是两个完全不同的东西. 然而凡人喜欢把它们揉在一起. 非阻塞的connect业务是个自带超时机制的 conn ...
- CSS原生布局方式
前言 网页原生布局的方法其实网上有很多,大概为Flow(流动布局模型).Float(浮动布局模型).Layer(层级布局模型).<!--more--> Flow布局 流动布局模型其实就是默 ...
- 关于多属性查找问题的sphinx解决方案
需求描述 mysql中,每一个文档都有多个标签,查询时可以筛选一个标签也可以筛选同时拥有多个标签的文档. 数据示例 文档 标签 1 1,2,3,4,5 2 2,3,4,5,6 3 3,4,5,6,7 ...