[POJ1155]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.

输入

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.

输出

The first and the only line of the output file should contain the maximal number of users described in the above text.

输入示例


输出示例


数据规模及约定

见“输入”;另:过程中不会有超过 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的更多相关文章

  1. poj1155 TELE (树上分组背包)

    题目链接:https://vjudge.net/problem/POJ-1155 题意:给定一颗以1为根的边权树,有n个结点,其中m个叶子结点,每个叶子结点有一个价值.要求从m个叶子结点中选最多的结点 ...

  2. poj1155 TELE (树上的背包)

    题目链接:http://poj.org/problem?id=1155 题意:给定一棵树,1为根结点表示电视台,有m个叶子节点表示客户,有n-m-1个中间节点表示中转站,每条树边有权值.现在要在电视台 ...

  3. POJ1155 TELE(树形DP)

    题目是说给一棵树,叶子结点有负权,边有正权,问最多能选多少个叶子结点,使从叶子到根的权值和小于等于0. 考虑数据规模表示出状态:dp[u][k]表示在u结点为根的子树中选择k个叶子结点的最小权值 最后 ...

  4. POJ-1155 TELE (树形DP+分组背包)

    题目大意:给一棵带边权的有根树,每个叶子节点有权.边权表示代价,叶子节点的权值代表可以补偿多少代价.问从根节点最多可以到达多少个叶子,使得付出的总代价不大于0. 题目分析:定义状态dp(u,k)表示从 ...

  5. POJ1155 - TELE(树形DP)

    题目大意 电视台要直播一场比赛,电视网络刚好形成了一棵树,其中有M个为客户端,其他的为中转站,其中中转站与中转站以及中转站与客户端之间连接都需要一定费用,每个客户i愿意支付pay[i]元钱,问电视台在 ...

  6. [POJ1155]TELE(树形背包dp)

    看到这道题的第一眼我把题目看成了TLE 哦那不是重点 这道题是树形背包dp的经典例题 题目描述(大概的): 给你一棵树,每条边有一个cost,每个叶节点有一个earn 要求在earn的和大于等于cos ...

  7. 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) 细节看代码: ...

  8. 【树形dp】TELE

    [POJ1155]TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5376   Accepted: 2973 Des ...

  9. [POJ 1155] TELE

    TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3787   Accepted: 2007 Description ...

随机推荐

  1. [dp]uestc oj E - 菲波拉契数制

    E - 菲波拉契数制 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  2. C 语言设计坦克大战(未完成)

    //坦克大战 //0.提示界面 //1.边框 //2.指定位置显示自己的坦克 //3.己方坦克随着方向键动起来 //getasynkeustae //Sleep(毫秒) //减少闪烁 //不闪烁Set ...

  3. Dojo的dojoConfig函数

    在我们引入 Dojo 的时候都会先做一些全局的配置,所使用的就是 Dojo 的 Config 接口. dojoConfig为以前的dgConfig函数. <script type="t ...

  4. rem和em的区别

    原文链接:http://caibaojian.com/rem-vs-em.html rem 单位如何转换为像素值 当使用 rem 单位,他们转化为像素大小取决于页根元素的字体大小,即 html 元素的 ...

  5. NOIP2016 toy

    题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...

  6. Visual Studio 2017 UTF-8 无 BOM 一站式解决办法

    问题背景:最近捡起C++,使用VS 2017平台.因为以前的编程习惯,喜欢使用UTF-8 无 BOM 的编码格式,好让自己的代码全球通用.但是VS 2017 对这个问题不是很友善.但最终找到了解决办法 ...

  7. pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'

    需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...

  8. Voyager的Roles和Pemissions

    以Page为例讲解: 取消admin的roles下Pages的Browse Pages权限: 打开web.php文件,添加: Route::get('pages', function(){ retur ...

  9. Beyond Compare4.x 破解方案

    如果过了30天的评估期或报“Beyond Compare 许可证密钥被撤销” 而导致不能再打开Beyond Compare,可以用下面的方法来解决: 1.找到“C:\Users\[Your User ...

  10. javascript实现原生ajax的几种方法介绍

    自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...