Teletime limit:1000msmemory limit:65536kbthis problem'll be judged onPKU. Original id:1155
64-bit integer IO format: %lld Java class name: Main 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 this emits the football match, the leaves of the tree is the potential users and OT Her vertices in the tree is 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 are the sum of prices of all individual signal transmissions.
Every user is 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 would find the maximal number of users able to watch the match so that the Tv-network ' s doesn ' t lose m Oney from broadcasting the match.InputThe first line of the input file contains integers n and M, 2 <= n <=, 1 <= M <= N-1, the number of V Ertices in the tree and the number of potential users.
The root of the tree is marked with the number 1, while other transmitters be numbered 2 to n-m and potential users are N Umbered 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 a transmitter transmits the signal to K transmitters or users, every one of the them described by the pair of Numbe RS A and C, the transmitter or user's number and the cost of transmitting the signal to them.
The last line contains the data on users, containing M integers representing respectively the price every one of them I s willing to pay to watch the match.OutputThe first and the only line of the output file should contain the maximal number of users described in the above text.Sample Input
9 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1
Sample Output
5
SourceCroatia OI 2002 Final Exam-second Day Problem Solving: Tree-shaped dp,dp[u][j] represents a subtree with the root of U for J customer Service
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMAXN =3005;6 Const intINF =0x3f3f3f3f;7 intdp[maxn][maxn],cnt[maxn],val[maxn],n,m;8 structarc{9 intTo,w,next;TenArcintx =0,inty =0,intz =-1){ Oneto =x; AW =y; -Next =Z; - } the}e[maxn<<1]; - intHead[maxn],tot; - voidAddintUintVintW) { -E[tot] =arc (V,w,head[u]); +Head[u] = tot++; - } + voidDfsintu) { ACnt[u] =1; at for(inti =1; I <= m; ++i) -Dp[u][i] =-INF; -dp[u][0] =0; - if(Head[u] = =-1){ -dp[u][1] =Val[u]; - return; in } - for(inti = Head[u]; ~i; i =E[i].next) { to DFS (e[i].to); +Cnt[u] + =cnt[e[i].to]; - for(intj = Cnt[u]; J >=1; --j) { the for(intK =0; K < J; ++k) *DP[U][J] = max (dp[u][j],dp[u][k]+dp[e[i].to][j-k]-E[I].W); $ }Panax Notoginseng } - } the intMain () { + intv,w,k; A while(~SCANF ("%d%d",&n,&m)) { thememset (head,-1,sizeofhead); + for(inti =1; I <= n-m; ++i) { -scanf"%d",&k); $ while(k--){ $scanf"%d%d",&v,&W); - Add (i,v,w); - } the } - for(inti = n-m +1; I <= N; ++i)Wuyiscanf"%d", val+i); theDfs1); - intAns =0; Wu for(inti = m; I >=1; --i) - if(dp[1][i] >=0) { AboutAns =i; $ Break; - } -printf"%d\n", ans); - } A return 0; +}
View Code
POJ 1155 TELE