标签:des style blog http java color
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13988 Accepted Submission(s): 5887
#include <stdio.h> #include <string.h> #include <algorithm> #define N 350000 #define INF 0x3f3f3f3f using namespace std; int dp[N],val[505],w[505],num[505]; int main() { int n,i,j,t,V,k; scanf("%d",&t); while(t--) { scanf("%d %d",&V,&n); for(i = 0 ; i < n ; i++) scanf("%d %d %d",&w[i],&val[i],&num[i]); memset(dp,0,sizeof(dp)); for(i = 0 ; i < n ; i++) { for(j = 1 ; j <= num[i];j++) { for(k = V;k >=w[i];k--) dp[k] = max(dp[k],dp[k-w[i]]+val[i]); } } printf("%d\n",dp[V]); } return 0; }
用二进制优化
#include <stdio.h> #include <string.h> #include <algorithm> #define N 350000 #define INF 0x3f3f3f3f using namespace std; int dp[N],val[505],w[505],num[505],cost[N],wei[N]; int main() { int n,i,j,t,V,k,cout; scanf("%d",&t); while(t--) { cout = 0; scanf("%d %d",&V,&n); for(i = 0 ; i < n ; i++) scanf("%d %d %d",&w[i],&val[i],&num[i]); memset(dp,0,sizeof(dp)); for(i = 0 ; i < n ; i++) { int ans = num[i]; for(k = 1 ; k<= ans ; k<<=1) { cost[cout] = k*w[i]; wei[cout++] = k*val[i]; ans-=k; } if(ans>0) { cost[cout] = ans*w[i]; wei[cout++] = ans*val[i]; } } for(i = 0 ; i <cout ; i++) for(j = V ; j>=cost[i] ; j--) dp[j] = max(dp[j],dp[j-cost[i]]+wei[i]); printf("%d\n",dp[V]); } return 0; }
标签:des style blog http java color
原文地址:http://www.cnblogs.com/llei1573/p/3850192.html