二维费用背包&状态机

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1001, M = 501;
int f[N][M];

int main()
{
    int V1, V2, n;
    cin>>V1>>V2>>n;
    for(int i = 0;i < n;i++)
    {
        int v1, v2;
        cin>>v1>>v2;
        for(int j = V1; j >=v1; j--)
            for(int k = V2 - 1; k >= v2; k--)
                f[j][k] = max(f[j][k], f[j-v1][k-v2] + 1);
    }
    
    cout<<f[V1][V2 - 1]<<' ';
    int k = V2 - 1;
    while(k > 0 && f[V1][k - 1] == f[V1][V2 - 1]) k--;
    cout<<V2 - k<<endl;
}

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/longxue1991/p/12784496.html