博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ1155]TELE
阅读量:5276 次
发布时间:2019-06-14

本文共 3741 字,大约阅读时间需要 12 分钟。

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

输入示例

9 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1

输出示例

5

数据规模及约定

见“输入”;另:过程中不会有超过 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 
#include
#include
#include
#include
#include
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 2147483647int 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;}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7309030.html

你可能感兴趣的文章
Linux中防火墙centos
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
centos下同时启动多个tomcat
查看>>
Leetcode Balanced Binary Tree
查看>>
[JS]递归对象或数组
查看>>
linux sed命令
查看>>
程序存储问题
查看>>
优雅地书写回调——Promise
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
css & input type & search icon
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
0320-学习进度条
查看>>
MetaWeblog API Test
查看>>
移动、尺寸改变
查看>>
c# 文件笔记
查看>>