nyoj-311-完全背包

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 int dp[50010];
 7 int main()
 8 {
 9     int n,m,v,c,w,i,j;
10     scanf("%d",&n);
11     while(n--)
12     {
13         scanf("%d%d",&m,&v);
14         memset(dp,-100,sizeof(dp));
15         dp[0]=0;
16         for(i=0;i<m;i++)
17         {
18             scanf("%d%d",&c,&w);
19             for(j=c;j<=v;j++)
20             dp[j]=dp[j-c]+w>dp[j]?dp[j-c]+w:dp[j];
21         }
22         if(dp[v]<0)
23         printf("NO
");
24         else
25         printf("%d
",dp[v]);
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/nylg-haozi/p/3193897.html