[2010] Fujian received rice

The farmer has n blocks of farmland, farmland planted with rice. Autumn, cooked rice, and each has a certain amount of rice farmland. We can farm as n points, numbered from 1 to n. No. 1 is the starting point for the farmer. Exactly n-1 road connecting these points, the length of each road is 1, and any two points are reachable. Each road has a certain length. Now the farmer from the starting point, to farmland harvesting rice. After each piece of farmland farmer will be able to reap the farmland of rice. But the farmer is so lazy, he did not want the total distance traveled more than m. How should choose a farmer harvesting the rice scheme up to get. Finally, the farmer can stop at any point!

Input

      The first line of a positive integer n (1 <= n <= 100) represents the number of farmland;

      The second line n integers (not exceeding 1000) indicates the number of each rice cropland.

  Then n-1 lines each two integers a, b, indicates there is a length of a path between a and b, the road is bidirectional.
 The last line of an integer m (0 <= m <= 200) represents the m most away from the farmer.

2
1 1
1 2
1

 

Output

     Output an integer representing the rice farmer to get up.

2

 

The data range of topics, n <= 100, m <= 200, thought tree dp.

1 so that the root, provided G [i] [j] denotes the subtree rooted at i, the maximum number of wheat as a starting point to i, j take up distance obtained.

years = g [1] [m]

But not only set a write transfer equation, such as node i, it may go a son y1, and then return to i, and then go to the other son y2.

So here a set of auxiliary array f

Set f [i] [j] denote the i sub-tree rooted in, starting with i, j up to go away, i went back, to get the maximum number of wheat.

Let y i is a son of

Equation: (y then go back)  G [I] [J] = max (G [I] [J], G [I] [JK-2] + F [y] [K]);

                 ( Do not come back down Y ) G [I] [J] = max (G [I] [J], F [I] [JK-. 1] + G [Y] [K]);

                                            f[i][j]=max(f[i][j],f[i][j-k-2]+f[y][k]);

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 int n,m,cnt;
 9 int h[105];
10 int v[105];
11 int f[105][205],g[105][205];
12 struct node{int to,next;}a[205];
13 void add(int x,int y){cnt++;a[cnt].to=y;a[cnt].next=h[x];h[x]=cnt;}
14 void dp(int x,int fa)
15 {
16     int i,j,k,y;
17     for(i=0;i<=m;i++)f[x][i]=g[x][i]=v[x];
18     for(i=h[x];i;i=a[i].next)
19     {
20         y=a[i].to;
21         if(y==fa)continue;
22         dp(y,x);
23         for(j=m;j>=0;j--)
24           for(k=0;k<=j-2;k++)
25             g[x][j]=max(g[x][j],g[x][j-k-2]+f[y][k]);
26         for(j=m;j>=0;j--)
27           for(k=0;k<=j-1;k++)
28             g[x][j]=max(g[x][j],f[x][j-k-1]+g[y][k]);
29         for(j=m;j>=0;j--)
30           for(k=0;k<=j-2;k++)
31             f[x][j]=max(f[x][j],f[x][j-k-2]+f[y][k]);  
32     }
33 }
34 int main()
35 {
36     int n,x,y,i;
37     cin>>n;
38     for(i=1;i<=n;i++)scanf("%d",&v[i]);
39     for(i=1;i<=n-1;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
40     cin>>m;
41     dp(1,0);
42     cout<<g[1][m];
43     return 0;
44 }

 

  

 

Guess you like

Origin www.cnblogs.com/dsb-y/p/11427379.html