$ POJ1015 \ Jury \ Compromise \ Dp $ / backpack

 Luo Gu Portal

 

$Sol$

This is one having a plurality of "volume dimensions" $ 0 / $ 1 knapsack problem.

The $ N $ N $ $ considered as candidates number items, each item has the following three volumes:

1. "number" of each candidate "number" is $ 1 $, and ultimately to fill the volume of $ M $ backpack

2. "defense score", $ A [I] $

3. "anti-party score", $ B [I] $

The defense is required out of $ D $ and $ P $ prosecution out the absolute value of the difference between the $ | the DP | $ minimum, if the selection method is not unique, then select $ D + P $ maximum program

Therefore, the $ a [i] -b [i ] $ as one volume, the $ a [i] + b [ i] $ as the value of the article

Note here $ a [i] -b [i ] $ may be negative, so that uniform plus $ 4000 $

In consideration of the $ I $ individuals stage, $ f [j] [k ] $ $ I $ denotes have been previously selected for Custom $ J $ individuals, this time difference is out of both of $ k $ time , both of the maximum score.

$f[j][k]=max(f[j][k],f[j-1][k-(a[i]-b[i])]+a[i]+b[i])$

Since the required output scheme, the array plus a $ d [i] [j] [k] $ denotes $ f [i] [j] [k] $ $ I $ of candidates is not selected from the group selected from the group, so that the recording program.

 

$Code$

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define Rg register
 5 #define il inline
 6 #define mem(a,b) memset(a,b,sizeof(a));
 7 #define go(i,a,b) for(Rg int i=a;i<=b;i++)
 8 #define yes(i,a,b) for(Rg int i=a;i>=b;i--)
 9 using namespace std;
10 il int read()
11 {
12     int x=0,y=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
15     return x*y;
16 }
17 int T,n,m,hhh=400,t,ct,a[201],b[201],f[21][801],as[21];
18 bool d[201][21][801];
19 int main()
20 {
21     while(1)
22     {
23         n=read(),m=read();if(!n)break;
24         printf("Jury #%d\n",++T);
25         go(i,1,n)a[i]=read(),b[i]=read();
26         mem(f,-777);mem(d,0);
27         f[0][hhh]=0;
28         go(i,1,n)
29             yes(j,m,1)
30             go(k,-400,400)
31                 {
32                     if(k-(a[i]-b[i])>=-400 && k-(a[i]-b[i])<=400 && f[j-1][k-(a[i]-b[i])+hhh]+a[i]+b[i]>=0 && f[j][k+hhh]<f[j-1][k-(a[i]-b[i])+hhh]+a[i]+b[i])
33                         f[j][k+hhh]=f[j-1][k-(a[i]-b[i])+hhh]+a[i]+b[i],d[i][j][k+hhh]=1;
34                     //if(f[j][k+hhh]>=0)cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<" f:"<<f[j][k+hhh]<<" d:"<<d[i][j][k+hhh]<<endl;
35                 }
36         go(i,0,400)
37         {
38             if(f[m][hhh+i]>=0){if(f[m][hhh+i]>f[m][hhh-i])t=i;else t=-i;break;}
39             else if(f[m][hhh-i]>=0){t=-i;break;}
40         }
41         printf("Best jury has value %d for prosecution and value %d for defence:\n",(f[m][hhh+t]+t)/2,(f[m][hhh+t]-t)/2);
42         int nw=n;ct=m;
43         while(nw>0)
44         {
45             int dd=d[nw][ct][t+hhh];
46             if(dd)as[ct]=nw,ct--,t-=(a[nw]-b[nw]);
47             nw--;
48         }
49         go(i,1,m)printf(" %d",as[i]);printf("\n\n");
50     }
51     return 0;
52 }
View Code

 

Guess you like

Origin www.cnblogs.com/forward777/p/10993789.html