标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4229 Accepted Submission(s): 2205
Sample Input
// // main.cpp // hdu2639 // // Created by Candy on 9/22/16. // Copyright © 2016 Candy. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=105,V=1005,K=35,INF=1e9+5; int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } int T,n,m,k,w[N],v[N]; int f[V][K],a[K],b[K]; void dp(){ memset(f,0,sizeof(f)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) for(int j=m;j>=v[i];j--){ for(int z=1;z<=k;z++) {a[z]=f[j][z];b[z]=f[j-v[i]][z]+w[i];} int x=1,y=1,z=1; while(z<=k&&(x<=k||y<=k)){ if(a[x]>=b[y]) f[j][z]=a[x++]; else f[j][z]=b[y++]; if(f[j][z]!=f[j][z-1]) z++; } } } int main(int argc, const char * argv[]) { T=read(); while(T--){ n=read();m=read();k=read(); for(int i=1;i<=n;i++) w[i]=read(); for(int i=1;i<=n;i++) v[i]=read(); dp(); printf("%d\n",f[m][k]); } return 0; }
HDU2639Bone Collector II[01背包第k优值]
标签:
原文地址:http://www.cnblogs.com/candy99/p/5898329.html