POJ 3624 (DP)

题目:http://poj.org/problem?id=3624

传说中的01背包问题啊!

代码:

#include <stdio.h>
#include <stdlib.h>

int w[3500],v[3500];
int s[13000];
int max(int a,int b)
{
  return a>b? a:b;    
}
 
int main()
{
  int bag,limit,i,j;
  
     while( scanf("%d%d",&bag,&limit) != EOF )  
     {
        for(i = 0 ; i < bag ; ++i)       
         scanf("%d%d",w+i,v+i);    
        
         for(j = 0 ;j < bag; ++j)
         for(i = limit ; i >= w[j]; --i)
         {
            s[i] = max(s[i],s[i-w[j]]+v[j]);
         }
         
         printf("%d\n",s[limit]);
              
     }
     
 // system("pause");
  return 0;
}

原文地址:https://www.cnblogs.com/HpuAcmer/p/2492169.html