[JSOI 2016] Best Group (+01 tree backpack fractional programming)

4753: [Jsoi2016] Best Group

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 2003  Solved: 790
[Submit][Status][Discuss]

Description

JSOI informatics team a total of N candidates, these candidates from 1 to N number. Convenience, JYY number is 0. Each is headed by a candidate
No less than his recommended candidate Ri. If Ri = 0 indicates that the candidate is JYY own fancy. In order to ensure a harmonious team, JYY need to ensure that,
If recruited candidates i, then the candidate Ri "will certainly need in the team. Of course, JYY himself always in the team. Each candidate has
A battle value of Pi ", there is also a recruiting fee Si". K JYY hope to recruit candidates (JYY not own), to form a cost-effective team.
That is, the ratio of the total value of fighting the K selected JYY recruit candidates with the total cost of the total maximum.
 

 

Input

Input line contains two positive integers K and N.
Next N lines, where the i-th row contains three integers Si, Pi, Ri i represents recruiting expenses of candidates, the battle and recommended number of people.
To 100% of the data satisfies 1≤K≤N≤2500,0 < "Si, Pi" ≤10 ^ 4,0≤Ri <i
 
 

 

Output

Output line a real number, the best ratio. Answers to three decimal places.
 

 

Sample Input

1 2
1000 1 0
1 1000 1

Sample Output

0.001
answer:
This is a problem that is beginning to see the road the water problem, and the trees on the stain that question, is a classic O $ (the n-^ 2 ) $ tree backpack, but this question is the transfer of two different elements, and the relationship between these two elements is made rather than simply the ratio of the sum, a shift began to think two respectively, but these two are the same thing he selected, can not maintain a normal way, then thought for a long time how were transfer and found very good transfer, is required to open a new label found cheese points: 01 points plan, but he will not ah qwq, online learning a bit, looked at do not think it is very difficult, outline the pair of.
01 Score plan:

01 planning score, simple terms, is that some tuples $ (s_i, p_i) $, select from a number of tuples, so $ \ Sigma {s_i} / \ Sigma {p_i} $ maximum (minimum).

This problem is a general class of solution, we assume $ x = \ Sigma {s_i} / \ Sigma {p_i} $ maximum (minimum) value, then there is $ x * \ Sigma {p_i} = \ Sigma {s_i } $, i.e. $ \ Sigma {s_i} -x * \ Sigma {p_i} = 0 $. That is, when a certain value of x satisfies the above equation, it is the desired value. We can think of enumeration ...... but then again, think about it, this can be half the answer.

So we direct answer half, when the above formula> 0, indicating little answer, <0 then the answer is big, so you can calculate.


 

Well, the pre-cheese resolved, that in fact this question is the Word 01 scores planning and $ (n ^ 2) $ backpack bare tree title of O, Also to be noted is the initialization problem, particular attention is that each second-half to give the answer must attach an array initial value dp, dp fact, each check is an array that is an array of values ​​dp x. Finally, note that the title because he says he must choose, and not included in the total number of people, so k ++.

The total time complexity $ O (n ^ 2logn) $, often slightly card, to open the luogu O2.

Sahua end.

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<vector>
 7 #include<queue>
 8 #include<cstdlib>
 9 using namespace std;
10 const int N=2505;
11 const double eps=1e-5;
12 int first[N],nex[N<<1],to[N<<1],tot;
13 int vis[N],size[N];
14 int n,k;
15 int att[N],co[N],ri[N];
16 double TerStegen[N];
17 double f[N][N];
18 void add(int a,int b){ to[++tot]=b,nex[tot]=first[a],first[a]=tot;}
19 void dfs(int x){
20     vis[x]=1;size[x]=1;
21     f[x][1]=TerStegen[x];//
22     for(int i=first[x];i;i=nex[i]){
23         int y=to[i];
24         //if(vis[y]) continue;//danxiangbian
25         dfs(y);
26         for(int j=min(size[x],k);j>=1;j--) for(int l=min(size[y],k);l>=1;l--) f[x][l+j]=max(f[x][l+j],f[y][l]+f[x][j]);//dayudengyu1?
27         size[x]+=size[y];
28     }
29 }
30 int check(double mid){
31     int ju;
32     for(int i=0;i<=2501;i++) for(int j=0;j<=2501;j++) f[i][j]=-88484848;
33     //memset(f,0xcf,sizeof(f));
34     for(int i=0;i<=n;i++) f[i][0]=0.0;
35     for(int i=0;i<=n;i++) TerStegen[i]=1.0*att[i]-1.0*mid*1.0*co[i];
36     dfs(0);
37     //cout<<f[0][k]<<endl;
38     if(f[0][k]>=0) ju=1;
39     else ju=0;
40     return ju;
41 }
42 int main(){
43     scanf("%d%d",&k,&n);
44     k++;//duliu
45     for(int i=1;i<=n;++i){
46         scanf("%d%d%d",&co[i],&att[i],&ri[i]);
47         add(ri[i],i);
48     }
49     double l=0.0,r=2e7*1.0;
50     double ans;
51     while(l+eps<r){
52         double mid=(l+r)/2;
53         //cout<<mid<<" "<<l<<" "<<r<<endl; 
54         if(check(mid)) l=mid;
55         else r=mid;
56     }
57     printf("%.3lf",(l+r)/2);
58 }
View Code

 

Guess you like

Origin www.cnblogs.com/leom10/p/11440330.html