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
解題:dp[p][m]:表示節點p上有m個user的最大剩於價值。
dp[p][m]=max(dp[p][m],dp[p][m-k]+dp[son][k]-cost);
最後只要最大剩於價值大於等於0,其中輸出最多user個數即可。
#include
#include
#include
using namespace std;
#define INF -9999999
typedef struct nnn
{
int id,cost;
}node;
vectormap[3005];
int dp[3005][3005],val[3005],k[3005],vist[3005],N,M;
int max(int a,int b)
{
return a>b?a:b;
}
void dfs(int p)
{
vist[p]=1; k[p]=0;
for(int i=0;i<=N;i++)
dp[p][i]=INF;
dp[p][0]=0;
if(p>N-M) dp[p][1]=val[p],k[p]=1;//表示葉子節點
for(int i=0;i