HDU 2602 01背包

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int main()
 6 {
 7     int i,j,N,V,v,ncases;
 8     int f[1005],c[1005],w[1005];
 9     
10     scanf("%d",&ncases);
11     
12     while( ncases-- )
13     {
14        scanf("%d%d",&N,&V);
15        for(i=1; i<=N; i++)
16          scanf("%d",&w[i]);
17        for(i=1; i<=N; i++)
18          scanf("%d",&c[i]);
19        memset(f,0,sizeof(f));
20          
21        for(i=1; i<=N; i++)
22         for(v=V; v>=0; v--)
23         {
24             if(v-c[i] >= 0)
25            f[v] = f[v] > f[v-c[i]]+w[i] ? f[v] : f[v-c[i]]+w[i];   
26         } 
27          
28         printf("%d\n",f[V]);
29     }
30     system("pause");
31     return 0;
32 }
33            

 

posted @ 2012-08-13 17:17  zhongya  阅读(130)  评论(0编辑  收藏  举报