poj1155 TELE(樹形dp+背包)
TELE
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 4344
Accepted: 2352
Description
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.
Input
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.
Output
The first and the only line of the output file should contain the maximal number of users described in the above text.
Sample Input
9 6
3 2 2 3 2 9 3
2 4 2 5 2
3 6 2 7 2 8 2
4 3 3 3 1 1
Sample Output
5
Source
Croatia OI 2002 Final Exam - Second Day
題意:給定一棵樹,1為根結點表示電視台,有m個葉子節點表示客戶,有n-m-1個中間節點表示中轉站,每條樹邊有權值。現在要在電視台播放一場比賽,每個客戶願意花費cost[i]的錢觀看,而從電視台到每個客戶也都有個費用,並且經過一條邊只會產生一個費用。問電視台不虧損的情況最多有幾個客戶可以看到比賽 分析:本題在思考的時候可分兩種情況:1、當節點為葉子節點時,每個用戶都要支付Money,那當前選一個的價值為Money值,中轉站和電視台要計算的是在保證不虧損也就是從用戶那獲得得價值減去中轉費用必須大等於0,那麼葉子節點的Money值就是基本信息。2、當節點為中轉站或者非葉子節點時,每次從子節點中選擇n個並計算收取的Money和中轉費用,如果兩者想減大等於0就用n來更新答案。怎麼轉換到分組背包呢?可以這樣想,每次從每個子節點中都只能取若干個,不可能重疊著取,那幾個節點就是幾組背包,最大容量是這點的葉子子孫數量,選幾個節點就是選擇的容量,價值就是用戶給的Money-中轉費用。
#include
#include
#include
#include
#include
#include