Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5045 Accepted Submission(s): 1595
New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:
Contain one number. (It is less then 2^31.)
1 3 2 10 11 100 1 2 9 1
3
二維狀態的01背包,且有一維要正好裝滿。
最後輸出時,因為時間為L時,f[m][L]不一定正好裝有物品,故要從所有f[m][ ]中查找滿足條件的最大值。
#include#include #include #define N 105 const int inf=-0x7fffffff; int f[N][N*10]; int max(int a,int b) { return a>b?a:b; } int main() { int i,j,k,n,m,l,T; int c[N],w[N]; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&l); for(i=0;i<=m;i++) for(j=0;j<=l;j++) f[i][j]=inf; f[0][0]=0; //因為要恰好m個影片,故要有一維正好裝滿 for(i=0;i 0;j--) { for(k=l;k>=c[i];k--) { f[j][k]=max(f[j][k],f[j-1][k-c[i]]+w[i]); } } } int ans=0; for(i=1;i<=l;i++) //最後要在所有滿足m個影片中找最大值 ans=max(ans,f[m][i]); if(ans>0) printf("%d\n",ans); else printf("0\n"); } return 0; }