标签:complete ++ question users pac tput ble ems ref
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2856 Accepted Submission(s): 1018
/* 二进制优化的时候,转化成完全背包就错,注释掉就对。神奇 */ #include<bits/stdc++.h> #define N 10005 #define M 11000 using namespace std; int dp[M]; void ZeroOne_Pack(int cost,int weight,int n) { for(int i=n;i>=cost;i--) dp[i]=max(dp[i],dp[i-cost]+weight); } void Complete_Pack(int cost,int weight,int n) { for(int i=cost;i<=n;i++) dp[i]=max(dp[i],dp[i-cost]+weight); } int Multi_Pack(int c[],int w[],int num[],int n,int m) { memset(dp,0,sizeof (dp)); for(int i=1;i<=n;i++) { // if(w[i]*num[i]>m) // { // Complete_Pack(c[i],w[i],m); // } // else // { //cout<<i<<endl; int k=1; while(k<num[i])//进行二进制拆分 { ZeroOne_Pack(c[i]*k,w[i]*k,m); num[i]-=k; k<<=1; } ZeroOne_Pack(c[i]*num[i],w[i]*num[i],m); // } } return dp[m]; } char op[20]; int main() { //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin); int n,m; int val,weight; int num[15][15]; while(scanf("%d%d",&n,&m)!=EOF) { memset(num,0,sizeof num); for(int i=1;i<=n;i++) { scanf("%s%d%d",op,&weight,&val); num[weight][val]++; } int Num[N],V[N],W[N]; memset(V,0,sizeof V); memset(W,0,sizeof W); memset(Num,0,sizeof Num); int len=0; for(int i=0;i<=10;i++) for(int j=0;j<=10;j++) { if(num[i][j]>0) { //cout<<num[i][j]<<endl; len++; Num[len]=num[i][j]; V[len]=j; W[len]=i; //cout<<W[len]<<" "<<V[len]<<endl; } } //cout<<len<<endl; printf("%d\n",Multi_Pack(V,W,Num,len,m)); // for(int i=0;i<=m;i++) // cout<<dp[i]<<" "; // cout<<endl; } return 0; }
标签:complete ++ question users pac tput ble ems ref
原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/5994260.html