Los tree valley P1273 cable networks [dp]

Topic : https://www.luogu.org/problemnew/show/P1273

Meaning of the questions : a tree, a leaf node is a user, while daily spending represents a weight, each user has a value represents the money they would post.

Asked without a loss, you can select up to number of users, allowing them to get sent out from the root node (1) service.

Idea : have been very naive to think is to deal with the dfs root to each leaf node of the net, then backpack. Sucker [too]

However, the common node with the segment of the path of a subtree, are not repeated here counted.

So to tree dp, $ dp [i] [j] $ $ I $ expressed in the sub-tree rooted at $ J $ a selected node.

$dp[i][j] = max(dp[i][j], dp[i][j-k]+dp[son][k]-e.w)$

$ J $ in the range of $ i $ the number of children and grandchildren, this need dfs time statistics, $ k $ is the size range of $ son $ tree subtree.

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<vector>
 8 #include<cmath> 
 9 #include<stack>
10 #include<queue>
11 #include<iostream>
12 
13 #define inf 0x3f3f3f3f
14 using namespace std;
15 typedef long long LL;
16 typedef pair<int, int> pr;
17 
18 int n, m;
19 const int maxn = 3005;
20 int head[maxn], tot;
21 struct edge{
22     int to, w, nxt;
23 }e[maxn];
24 int pay[maxn];
25 int dp[maxn][maxn];
26 
27 void add(int x, int y, int c)
28 {
29     e[++tot].to = y;
30     e[tot].w = c;
31     e[tot].nxt = head[x];
32     head[x] = tot;
33 }
34 
35 int dfs(int now)
36 {
37     if(now > n - m){
38         dp[now][1] = pay[now];
39         return 1;
40     }
41     int sum = 0, son = 0;
42     for(int i = head[now]; i; i = e[i].nxt){
43         int to = e[i].to;
44         //printf("%d %d\n", now, e[i].to);
45         son = dfs(to);
46         sum += son;
47         for(int j = sum; j > 0; j--){
48             for(int k = 1; k <= son; k++){
49                 if(j >= k)dp[now][j] = max(dp[now][j], dp[now][j - k] + dp[to][k] - e[i].w);
50             }
51         }
52     }
53     return sum;
54 }
55 
56 int main()
57 {
58     memset(dp, ~0x3f, sizeof(dp));
59     memset(head, 0, sizeof(head));
60     scanf("%d%d", &n, &m);
61     for(int i = 1; i <= n - m; i++){
62         int k;
63         scanf("%d", &k);
64         while(k--){
65             int a, c;
66             scanf("%d%d", &a, &c);
67             add(i, a, c);
68         }
69         dp[i][0] = 0;
70     }
71     for(int i = n - m + 1; i <= n; i++){
72         scanf("%d", &pay[i]);
73         dp[i][0] = 0;
74     }
75     dfs(1);
76     for(int i = m; i > 0; i--){
77         if(dp[1][i] >= 0){
78             printf("%d\n", i);
79             break;
80         }
81     }
82 }

 

Guess you like

Origin www.cnblogs.com/wyboooo/p/11084698.html