#P1273. 有线电视网

    ID: 271 远端评测题 1000ms 125MiB 尝试: 1 已通过: 1 难度: 6 上传者: 标签>动态规划,dp树形结构递归背包

有线电视网

Description

A pay cable television network plans to rebroadcast an important football match. Their rebroadcast network and user terminals form a tree structure. The root of the tree is located at the match venue, the leaves are user terminals, and the other relay stations are internal nodes of the tree.

The signal transmission costs from relay station to relay station and from relay station to user terminals are known. The total cost of one broadcast equals the sum of all transmission costs used.

Each user has prepared a certain amount of money to watch the match. The cable network has the right to decide which users receive the signal and which do not.

Write a program to find a plan that allows the cable network to serve as many users as possible without incurring a loss.

Input Format

The first line contains two integers NN and MM separated by a space, where 2N30002 \le N \le 3000, 1MN11 \le M \le N - 1. Here NN is the total number of nodes in the entire cable network, and MM is the number of user terminals.

The first relay station (the root of the tree) is numbered 11. The other relay stations are numbered from 22 to NMN - M. The user terminals are numbered from NM+1N - M + 1 to NN.

The next NMN - M lines each describe one relay station. The (i+1)(i + 1)-th line describes relay station ii in the following format:

$K \ \ A_1 \ \ C_1 \ \ A_2 \ \ C_2 \ \ \ldots \ \ A_k \ \ C_k$

KK indicates that this relay station has KK direct children (relay stations or users). Each child is given by a pair of integers AA and CC, where AA is the child node number, and CC is the transmission cost from the current relay station to node AA.

The last line contains, in order, the amounts of money each user is willing to pay to watch the match. Each single transmission cost and each user's payment do not exceed 1010.

Output Format

Output a single integer on one line: the maximum number of users that can be served without the network incurring a loss.

5 3
2 2 2 5 3
2 3 2 4 3
3 4 2
2

Hint

Sample explanation:

As shown in the figure, there are five nodes in total. Node 1 is the root node, i.e., the live broadcast station. Node 2 is a relay station. Nodes 3, 4, 5 are user terminals, MM in total, numbered from NM+1N - M + 1 to NN. They are willing to pay 33, 44, and 22, respectively.

From node 1, the signal can be sent to node 2 at a cost of 22; it can also be sent to node 5 at a cost of 33 (as shown by the second line of data). From node 2, the signal can be sent to node 3 at a cost of 22; it can also be sent to node 4 at a cost of 33 (as shown by the third line of data).

If all users (3, 4, 5) are to watch the match, the total transmission cost is 2+3+2+3=102 + 3 + 2 + 3 = 10, which is greater than the total amount users are willing to pay 3+4+2=93 + 4 + 2 = 9, so the cable network would incur a loss. If only users 3 and 4 watch the match, then there is no loss.

Translated by ChatGPT 5