[POJ1155]TELE
[POJ1155]TELE
试题描述
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.
输入
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.
输出
输入示例
输出示例
数据规模及约定
见“输入”;另:过程中不会有超过 int 的值。
题解
树形 dp(树上背包)。
设 f(i, j) 表示子树 i 中选择了 j 个叶子的最大获利(若为负则 -f(i, j) 为最小亏损)。那么答案就是最大的 j,满足 f(i, j) 非负。
考虑子树 u,儿子上的信息肯定是最有子结构,所以先算出所有的 f(son, j),然后分别将一个个子树的信息加入 f(i, j)(f(u, i+j) = max{ f(u, i) + f(son, j) - dist(i, son) | j > 0 , f(u, i) + f(son, j) | j = 0 })。
可以证明总转移数是 O(n2) 级别的,详见这里。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 3010
#define oo 2147483647 int n, usr, m, head[maxn], nxt[maxn], to[maxn], dist[maxn], pay[maxn]; void AddEdge(int a, int b, int c) {
to[++m] = b; dist[m] = c; nxt[m] = head[a]; head[a] = m;
return ;
} int f[maxn][maxn], clea[maxn];
void dp(int u) {
if(u > n - usr) {
clea[u] = 1;
f[u][0] = 0; f[u][1] = pay[u];
return ;
}
f[u][0] = 0;
for(int e = head[u]; e; e = nxt[e]) {
dp(to[e]);
for(int i = clea[u]; i >= 0; i--) if(f[u][i] < oo)
for(int j = 0; j <= clea[to[e]]; j++) if(f[to[e]][j] < oo)
f[u][i+j] = max(f[u][i+j], f[u][i] + f[to[e]][j] - (j ? dist[e] : 0));
clea[u] += clea[to[e]];
}
return ;
} int main() {
n = read(); usr = read();
for(int i = 1; i <= n - usr; i++) {
int k = read();
while(k--) {
int u = read(), c = read();
AddEdge(i, u, c);
}
}
for(int i = n - usr + 1; i <= n; i++) pay[i] = read(); for(int i = 1; i <= n; i++)
for(int j = 0; j <= n; j++) f[i][j] = -oo;
dp(1); for(int j = clea[1]; j; j--) if(f[1][j] >= 0) return printf("%d\n", j), 0;
puts("0"); return 0;
}
[POJ1155]TELE的更多相关文章
- poj1155 TELE (树上分组背包)
题目链接:https://vjudge.net/problem/POJ-1155 题意:给定一颗以1为根的边权树,有n个结点,其中m个叶子结点,每个叶子结点有一个价值.要求从m个叶子结点中选最多的结点 ...
- poj1155 TELE (树上的背包)
题目链接:http://poj.org/problem?id=1155 题意:给定一棵树,1为根结点表示电视台,有m个叶子节点表示客户,有n-m-1个中间节点表示中转站,每条树边有权值.现在要在电视台 ...
- POJ1155 TELE(树形DP)
题目是说给一棵树,叶子结点有负权,边有正权,问最多能选多少个叶子结点,使从叶子到根的权值和小于等于0. 考虑数据规模表示出状态:dp[u][k]表示在u结点为根的子树中选择k个叶子结点的最小权值 最后 ...
- POJ-1155 TELE (树形DP+分组背包)
题目大意:给一棵带边权的有根树,每个叶子节点有权.边权表示代价,叶子节点的权值代表可以补偿多少代价.问从根节点最多可以到达多少个叶子,使得付出的总代价不大于0. 题目分析:定义状态dp(u,k)表示从 ...
- POJ1155 - TELE(树形DP)
题目大意 电视台要直播一场比赛,电视网络刚好形成了一棵树,其中有M个为客户端,其他的为中转站,其中中转站与中转站以及中转站与客户端之间连接都需要一定费用,每个客户i愿意支付pay[i]元钱,问电视台在 ...
- [POJ1155]TELE(树形背包dp)
看到这道题的第一眼我把题目看成了TLE 哦那不是重点 这道题是树形背包dp的经典例题 题目描述(大概的): 给你一棵树,每条边有一个cost,每个叶节点有一个earn 要求在earn的和大于等于cos ...
- POJ-1155 TELE 树形背包dp
dp[u][i]代表以u为根的子树选i个叶子的最大收益 那么dp[u][i]=max(dp[u][i],dp[v][k]+dp[u][i-k]-len) (1=<k<=i) 细节看代码: ...
- 【树形dp】TELE
[POJ1155]TELE Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5376 Accepted: 2973 Des ...
- [POJ 1155] TELE
TELE Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3787 Accepted: 2007 Description ...
随机推荐
- 通过WMIC导出系统日志
查看日志类型 wmic nteventlog get filename C:\>wmic nteventlog get filename FileName appevent secevent s ...
- scipy学习之(二)——io操作及其misc操作对图片的处理
SciPy有许多模块.类和函数,io子模块可用于从各种文件格式中读取数据和将数据写入各种文件格式. from scipy import io import numpy as np 生成数据 data ...
- (74)zabbix第三方认证之http(nginx basic auth)
HTTP Basic Auth认证方式,我们将在实例中使用nginx来演示,Apache也类似. zabbix认证配置 Administration>> Authentication,将h ...
- 【Linux命令】nohup和&差异,查看进程和终止进程!
最近在开发dueros的技能,官方提供的PHPSDK中有多个实力,而运行实例的命令如下是 nohup php -S 0.0.0.0:8029 myindex.php & 从命令来看,肯定是在8 ...
- Java并发编程的艺术 记录(二)
volatile的应用 volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量.Java语言提供了volatil ...
- Linux学习-使用传统程序语言进行编译的简单范例
单一程序:印出 HelloWorld 编辑程序代码,亦即原始码 [root@study ~]# vim hello.c <==用 C 语言写的程序扩展名建议用 .c #include <s ...
- jenkins匿名用户登录 - 安全设置
最近自己安装配置jenkins,但是跑任务时,发现是匿名账户登录,且提示: 后来发现搭建好jenkins之后,默认就是匿名用户登录的,可以在安装设置菜单里进行账户管理. 1.登录Jenkins服务器, ...
- hdu3487Play with Chain
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- BZOJ 4057: [Cerc2012]Kingdoms
状压DP #include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],st ...
- python 提交form-data之坑
#coding=utf-8 import requests from requests_toolbelt import MultipartEncoder #requests库上传 files = {& ...