bzoj.4753. [JSOI2016] Best Group (01 score Plan Tree Backpack DP)

Source: Internet
Author: User

Topic links

\ (description\)

Each point has a cost Si and value pi, which requires selecting some connected blocks with a root, the total size is k, making \ (\frac{∑pi}{∑si}\) the largest

\ (solution\)

01 Fractional plan, then DP, set F[I][J] indicates the maximum weight of the I subtree selected J and, direct violent backpack transfer can be
When enumerating the number of child nodes selected, assume X has 1.2.3.4 four child nodes with a complexity of \ (1*sz[1]+sz[1]*sz[2]+ (sz[1]+sz[2)) *sz[3]+ (sz[1]+sz[2]+sz[3]) *sz[4]\)
Equivalent to each pair of points in the LCA contribution, there will be a total of n^2 LCA, so this part of the complexity of O (n^2)
Total O (n^2*log ans)
Note: The initial value should not be 0 because there will be a large negative number. For example, must stipulate f[0][1] for-inf
Why EPS needs 1e-5. Is <1e-4 over? (perhaps because this is not an exact answer?) card when Dafa is good )

//50024 KB 3512 ms so slow. #include <cstdio>#include <cctype>#include <cstring>#include <algorithm>#define GC () GetChar ()Const intn=2505;Const Doubleeps=1e-5, inf=1e10;intN,m,enum,h[n],nxt[n],to[n],sz[n];DoubleCost[n],p[n],val[n],f[n][n];inline intRead () {intnow=0;Register CharC=GC (); for(;! IsDigit (c); C=GC ()); for(; IsDigit (c); now=now*Ten+c-' 0 ', C=GC ());returnNow;}inline voidAddedge (intUintV) {to[++enum]=v, nxt[enum]=h[u], h[u]=enum;}voidDFS (intx) {intMn= (x>0); SZ[X]=MN; for(intI=2; i<=m;    ++i) F[x][i]=-inf; f[x][1]=VAL[X]; for(intV,I=H[X]; I I=nxt[i]) {DFS (v=to[i]); for(intJ=SZ[X]; j>=mn; --J)//reverse//update upper limit is the sum of SZ and sub-node sz             for(intk=1; k<=sz[v];        ++k) F[x][j+k]=std::max (F[x][j+k],f[x][j]+f[v][k]);    SZ[X]+=SZ[V]; }}DoubleSolve (Doublex) { for(intI=1; i<=n; ++i) Val[i]=p[i]-x*cost[i];//memset (f,0xc2,sizeof f), F[0][0]=0;//too slowDFS (0);returnf[0][m]>=0;}intMain () {M=read (), N=read (); for(intI=1; i<=n; ++i) Cost[i]=read (), P[i]=read (), Addedge (read (), i);DoubleL=0.0, r=1e4, Mid; val[0]=-inf;//f[0][1]     while(r-l>eps) {if(Solve (mid= (l+r)/2.0)) L=mid;ElseR=mid; } printf ("%.3LF", mid);return 0;}

bzoj.4753. [JSOI2016] Best Group (01 score Plan Tree Backpack DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.