标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3718 Accepted Submission(s): 1903
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #include<stack> 6 #include<map> 7 #include<set> 8 #include<algorithm> 9 #define ll __int64 10 #define pi acos(-1.0) 11 #define mod 1 12 #define maxn 10000 13 using namespace std; 14 int t; 15 int n,v,k; 16 int a[105]; 17 int b[105]; 18 int we[105],va[105]; 19 int dp[1005][105]; 20 int main() 21 { 22 while(scanf("%d",&t)!=EOF) 23 { 24 for(int i=1;i<=t;i++) 25 { 26 memset(we,0,sizeof(we)); 27 memset(va,0,sizeof(va)); 28 memset(dp,0,sizeof(dp)); 29 scanf("%d %d %d",&n,&v,&k); 30 for(int j=1;j<=n;j++) 31 scanf("%d",&we[j]); 32 for(int j=1;j<=n;j++) 33 scanf("%d",&va[j]); 34 for(int j=1;j<=n;j++) 35 { 36 for(int l=v;l>=va[j];l--) 37 { 38 memset(a,0,sizeof(a)); 39 memset(b,0,sizeof(b)); 40 for(int m=1;m<=k;m++) 41 { 42 a[m]=dp[l][m]; 43 b[m]=dp[l-va[j]][m]+we[j]; 44 } 45 a[k+1]=-1; 46 b[k+1]=-1; 47 int x=1,y=1,w=1; 48 while(w<=k&&(x<=k||y<=k))//合并的过程 49 { 50 if(a[x]>b[y]) 51 { 52 dp[l][w]=a[x]; 53 x++; 54 } 55 else 56 { 57 dp[l][w]=b[y]; 58 y++; 59 } 60 if(w==1||dp[l][w]!=dp[l][w-1]) 61 w++; 62 } 63 } 64 } 65 cout<<dp[v][k]<<endl; 66 } 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5441013.html