SOJ 4580 Dynamic Programming 01 Backpack (01 Backpack)

Description

  Sidney wants to play at Gandtom's house. But between Sidney's and Gandtom's house is a rough, potholed dirt road. So he needs to pack a few bags of thin mud on his back and spread some dry soil on the road to make the road smooth and muddy before he can meet Gandtom at Gandtom's house. 
  It is known that there is a kind of thin mud, the mass of the first kind of thin mud is , the volume is , and the quantity is . Sidney's packs can hold no more than a volume of thin mud. The mass of the thin mud Sidney takes with him when he goes out should be as large as possible. Under this premise, the volume of the diluted mud carried should also be as large as possible. 
  Try to find the maximum mass of thin mud Sidney can carry and the maximum volume at this time on the road.

 

Input

The first line has an integer representing the number of groups. 
The first row of each set of data has two positive integers , The second row of each set of data has a positive integer, the th number is There is a positive integer in the third row of each set of data , and the number is . 

 

Output

The first line of each set of samples outputs two integers. Indicates how much mass of thin mud Sidney can carry at most and the maximum volume at this time on the road.

 

Sample Input


5 3 
1 2 3 4 5 
1 1 1 1 1 
3 7 
1 2 1 
3 5 3

 

Sample Output

12 3 
2 6

 

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int dp[1001],w[1001],v[1001];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(dp,0,sizeof(dp));
        int n,V,i,j,max1=0,maxpos=0;
        scanf("%d%d",&n,&V);
        for(i=1;i<=n;i++)
          scanf("%d",&w[i]);
        for(i=1;i<=n;i++)
          scanf("%d",&v[i]);
        dp[v[1]]=w[1];
        for(i=2;i<=n;i++)
          for(j=V;j>=v[i];j--)
            if(j==v[i]||dp[j-v[i]])
              dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
        for(i=V;i>0;i--)
          if(dp[i]>max1){
              max1=dp[i];
              maxpos = i;
          }
       printf("%d %d\n",max1,maxpos);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932463&siteId=291194637