Files
python/GESP/Level6/20230901_jyh.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

47 lines
973 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int v[N], w[N];
int n, L;
int ans = INT_MAX;
int memo[N][N]; // memo[u][m] 表示处理到第u个物品时,容量m的最小花费
void dfs(int u, int c, int m) {
// 剪枝:当前花费已超过已知最优解
if (c >= ans)
return;
// 统一处理容量达标的情况
m = min(m, L); // 状态压缩
if (m >= L) {
ans = min(ans, c); // 记录可行的解
return;
}
// 记忆化剪枝
if (memo[u][m] != -1 && c >= memo[u][m])
return;
memo[u][m] = c;
// 递归终止条件
if (u > n)
return;
// 选当前物品
dfs(u + 1, c + v[u], m + w[u]);
// 不选当前物品
dfs(u + 1, c, m);
}
int main() {
cin >> n >> L;
for (int i = 1; i <= n; i++)
cin >> v[i] >> w[i];
memset(memo, -1, sizeof memo);
dfs(1, 0, 0);
cout << (ans < INT_MAX ? to_string(ans) : "no solution") << endl;
return 0;
}