二维费用背包


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 5000
int f[M][M];
int n, m, k;
int a[M], b[M], c[M];
int main() {
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i <= k; i++) scanf("%d%d", &a[i], &b[i]);
	for(int i = 1; i <= k; i++) {
		for(int j = n; j >= a[i]; j--) {
			for(int l = m; l >= b[i]; l--) {
				f[j][l] = max(f[j][l], f[j-a[i]][l-b[i]]+1);
			}
		}
	}
	int ans;
	for(ans = 0; ans <= m; ans++) if(f[n][ans] == f[n][m]) break;
	printf("%d %d\n", f[n][m], m - ans);
	return 0;
}

相关文章:

  • 2021-12-20
  • 2021-08-31
  • 2021-06-04
  • 2022-01-01
  • 2022-01-01
  • 2021-12-02
  • 2021-10-01
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2021-08-28
  • 2022-01-14
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案