"Brush title" Best Group

  Initially thought was a sand sculpture idea, is nested dp, dp first linear process a bundle of everyone who must enter the team and (combat and costs), then followed by a linear dp processing the optimal solution, but found that the algorithm false, because after this thing is not no sex effect. Because a possible case where the numerator and denominator of the ratio are great great, while the other case is the same as the ratio of the numerator and denominator very large but smaller than the front, this is give it a little. But that small molecules obtained before now have a relatively small bundle of people, added to the large ratio will be larger than the join. This algorithm is dead.

  Then immediately thought of the 01 scores planning (title I have not played it), because this form really want to, up and down the numerator and denominator, the answer may be fractional-half, the accuracy is not very card dead, the only problem is bundled with people problems I can not separate them, but will not be considered separately from heavy, so you can use the tree to verify backpack.

  But be careful not to take the maximum value is the maximum value but take filled, that is loaded into the backpack is also negative. Such initial value at the beginning of the backpack directly conferred on the articles of this node, equivalent to compulsory purchase. Because the data is then merged to run sub-tree 2500, Xia Xie just fine.

 

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 const int maxn=2505,INF=1e9;
 7 int n,K,mx,a[maxn],b[maxn],sz[maxn],r[maxn],d[maxn];
 8 double c[maxn],dp[maxn][maxn],tmp[maxn];
 9 vector<int> ch[maxn];
10 void dfs(int x)
11 {
12     for(int i=0;i<ch[x].size();i++) dfs(ch[x][i]);
13     sz[x]=1;dp[x][1]=c[x];
14     for(int i=0;i<ch[x].size();i++)
15     {
16         int t=ch[x][i];
17         for(int j=0;j<=K;j++) tmp[j]=-INF;
18         for(int j=0;j<=min(sz[x],K);j++)
19             for(int k=0;k<=min(sz[t],K-j);k++)
20                 tmp[j+k]=max(dp[x][j]+dp[t][k],tmp[j+k]);
21         sz[x]+=sz[t];
22         for(int j=0;j<=min(sz[x],K);j++) dp[x][j]=max(tmp[j],dp[x][j]);
23     }
24 }
25 bool check(double mid)
26 {
27     for(int i=0;i<=n;i++)
28     {
29         c[i]=1.0*a[i]-mid*b[i];
30         for(int j=0;j<=K;j++) dp[i][j]=-INF;
31     }
32     dfs(0);
33     return dp[0][K]>=0;
34 }
35 int main()
36 {
37     scanf("%d%d",&K,&n);K++;
38     for(int i=1;i<=n;i++)
39     {
40         scanf("%d%d%d",&b[i],&a[i],&r[i]);
41         mx+=a[i];
42         ch[r[i]].push_back(i);
43     }
44     double l=0,r=1e7;
45     while(r-l>1e-4)
46     {
47         double mid=(l+r)/2;
48         if(check(mid)) l=mid;
49         else r=mid;
50     }
51     printf("%.3lf\n",l);
52     return 0;
53 }
Best Group

 

Guess you like

Origin www.cnblogs.com/Lrefrain/p/11272898.html