看的解题报告。。http://blog.csdn.net/lyy289065406/article/details/6671105
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 int dp[22][805]; 9 int path[22][805]; 10 int p[205],d[205],s[205],v[205]; 11 int ans[205]; 12 int n,m; 13 14 bool can(int j,int k,int i) 15 { 16 while(j>0) 17 { 18 if(path[j][k]==i) 19 return false; 20 k-=v[path[j][k]]; 21 j--; 22 } 23 return true; 24 } 25 26 int main() 27 { 28 int cas=1; 29 while(scanf("%d%d",&n,&m)) 30 { 31 if(!n && !m) break; 32 memset(dp,-1,sizeof(dp)); 33 memset(path,0,sizeof(path)); 34 for(int i=1;i<=n;i++) 35 { 36 scanf("%d%d",&p[i],&d[i]); 37 s[i]=p[i]+d[i]; 38 v[i]=p[i]-d[i]; 39 } 40 int fix=m*20; 41 dp[0][fix]=0; 42 for(int j=1;j<=m;j++) 43 { 44 for(int k=0;k<=2*fix;k++) 45 { 46 if(dp[j-1][k]>=0) 47 { 48 for(int i=1;i<=n;i++) 49 { 50 if(dp[j][k+v[i]] < dp[j-1][k] + s[i] && can(j-1,k,i)) 51 { 52 dp[j][k+v[i]]=dp[j-1][k]+s[i]; 53 path[j][k+v[i]]=i; 54 } 55 } 56 } 57 } 58 } 59 60 int minv=0; 61 for(int k=0;k<=fix;k++) 62 { 63 if(dp[m][fix+k]>=0 && dp[m][fix+k]>dp[m][fix-k]) 64 { 65 minv=fix+k; 66 break; 67 } 68 else if(dp[m][fix-k]>=0) 69 { 70 minv=fix-k; 71 break; 72 } 73 } 74 printf("Jury #%d\n",cas++); 75 printf("Best jury has value %d for prosecution and value %d for defence:\n",(dp[m][minv]+minv-fix)/2,(dp[m][minv]+fix-minv)/2); 76 for(int j=m,k=minv;j>0;j--) 77 { 78 ans[j]=path[j][k]; 79 k-=v[ans[j]]; 80 } 81 sort(ans+1,ans+m+1); 82 for(int i=1;i<=m;i++) 83 printf(" %d",ans[i]); 84 puts("\n"); 85 } 86 return 0; 87 }