[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. Iterator中的next()

    DBExchangeMoney类: 1 package com.ch.test15; import java.sql.DriverManager; import java.sql.ResultSet; ...

  2. UVA 1614 - Hell on the Markets 奇怪的股市(贪心,结论)

    先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可以凑出1~sum[i]中的任意一个数. 对于i=1显然成立, 假设对于i=k结论成立,那么对于i=k+1来说,只要证明sum[k]+i,1≤i ...

  3. zend studio failed to create java virtual machine无法启动的解法

    zend studio failed to create java virtual machine 解决方案:在安装目录下修改ZendStudio.ini中第十四行处改成 -Xmx512M. -sta ...

  4. Java写诗程序

    import java.util.Random; public class test_word { public static void main(String[] args) { System.ou ...

  5. 删除sqlserver管理器登录信息缓存

    在Windows10下测试有效: C:\Users\<user>\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shel ...

  6. 01_1_jdom调用xml文件

    01_1_jdom调用xml文件 1. 导入jdom.jar包 2. xml文件内容 test.xml <?xml version="1.0" encoding=" ...

  7. JavaScript中数组的使用

    ---恢复内容开始--- 创建数组 1,通过 var arr1 = [1,2,3] 2通过使用 var arr2 = new Array(1,2,3) 在这里创造的数组实际上都是一个对象,然后把对象的 ...

  8. CF-1093 (2019/02/10)

    CF-1093 1093A - Dice Rolling 输出x/2即可 #include<bits/stdc++.h> using namespace std; int main() { ...

  9. 事务控制语言DTL

    一.什么是事务? · 数据库中的事务,是指可以将“多条相关语句执行”看做是“一条语句执行”的一种内部机制.即事务是一种可以保证“多条语句一次性执行完成”或者一条语句都不执行的机制. 三.事务的特点 原 ...

  10. 第6章 AOP与全局异常处理6.5-6.11 慕课网微信小程序开发学习笔记

    https://coding.imooc.com/learn/list/97.html 目录: 第6章 AOP与全局异常处理6-1 正确理解异常处理流程 13:236-2 固有的处理异常的思维模式与流 ...