POJ1155 TELE

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4976   Accepted: 2734

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

树形DP,f[当前结点][连接的客户端数]=花费

最后对f[根节点][j]扫描,找到使f为正的最大j,即是答案。

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 const int mxn=3200;
10 struct edge{
11     int v,dis;
12     int nxt;
13 }e[mxn];
14 int hd[mxn],cnt=0;
15 void add_edge(int u,int v,int dis){
16     e[++cnt].v=v;e[cnt].dis=dis;e[cnt].nxt=hd[u];hd[u]=cnt;
17     return;
18 }
19 int n,m;
20 int num[mxn];
21 int f[mxn][mxn];
22 void dp(int u){
23     int i,j,k;
24     for(i=hd[u];i;i=e[i].nxt){
25         int v=e[i].v;
26         dp(v);
27         num[u]+=num[v];
28         for(j=num[u];j;--j){
29             for(k=num[v];k;--k){
30                 f[u][j]=max(f[u][j],f[u][j-k]+f[v][k]-e[i].dis);
31             }
32         }
33     }
34     return;
35 }
36 int main(){
37     scanf("%d%d",&n,&m);
38     int i,j,k;
39     int u,v,d;
40     for(i=1;i<=n-m;i++){
41         scanf("%d",&k);
42         for(j=1;j<=k;j++){    
43             scanf("%d%d",&v,&d);
44             add_edge(i,v,d);
45         }
46     }
47     for(i=1;i<=n;i++)
48      for(j=1;j<=m;j++)
49          f[i][j]=-INF;
50     for(i=n-m+1;i<=n;i++){num[i]=1;scanf("%d",&f[i][1]);}
51     dp(1);
52     for(i=m;i>=0;i--){
53         if(f[1][i]>=0){
54             printf("%d
",i);
55             break;
56         }
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/SilverNebula/p/5916227.html