完全背包

https://nanti.jisuanke.com/t/41401

Dawn-K recently discovered a very magical phenomenon in the supermarket of Northeastern University: The large package is not necessarily more expensive than the small package.

On this day, Dawn-K came to the supermarket to buy mineral water, he found that there are nnn types of mineral water, and he already knew the price ppp and the weight ccc (kg) of each type of mineral water. Now Dawn-K wants to know the least money aaa he needs to buy no less than mmm kilograms of mineral water and the actual weight bbb of mineral water he will get. Please help him to calculate them.

Input

The input consists of multiple test cases, each test case starts with a number nnn (1≤n≤1031 \le n \le 10^31n103) -- the number of types, and mmm (1≤m≤1041 \le m \le 10^41m104) -- the least kilograms of water he needs to buy. For each set of test cases, the sum of nnn does not exceed 5e45e45e4.

Then followed n lines with each line two integers ppp (1≤p≤1091 \le p \le 10^91p109) -- the price of this type, and ccc (1≤c≤1041 \le c \le 10^41c104) -- the weight of water this type contains.

Output

For each test case, you should output one line contains the minimum cost aaa and the weight of water Dawn-K will get bbb. If this minimum cost corresponds different solution, output the maximum weight he can get.

(The answer aaa is between 111 and 10910^9109, and the answer bbb is between 111 and 10410^4104)

样例输入

3 3
2 1
3 1
1 1
3 5
2 3
1 2
3 3

样例输出

3 3
3 6
https://blog.csdn.net/qq_38984851/article/details/81133840
//#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include 
#include <set>
#include <string.h>
#include 
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int p[1009] , w[1009];
int dp[20009];



int main()
{
    int n , v ;
    while(~scanf("%d%d" , &n , &v))
    {
        for(int i = 1 ; i <= n ; i++)
        {
            scanf("%d%d" , &p[i] , &w[i]);
        }
        for(int i = 1 ; i <= 10009 ; i++)
            dp[i] = INF;
        dp[0] = 0;
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = w[i] ; j <= 10009 ; j++)
            {
                dp[j] = min(dp[j] , dp[j-w[i]]+p[i]);
            }
        }
        int ans = INF , index = 0 ;
        for(int i = v ; i <= 10009 ; i++)
        {
            if(dp[i] < ans)
            {
                ans = dp[i];
                index = i ;
            }
            else if(dp[i] == ans && index < i)
            {
                index = i ;
            }
        }
        cout << ans << " " << index << endl ;
    }

    return 0;
}

 

你可能感兴趣的:(完全背包)