Files
python/GESP/Level6/20230901_dfs.cpp

49 lines
829 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
/*
5 100
100 2000
2 50
4 40
5 30
3 20
答案:9
*/
int v[N], w[N];
int n, L;
int ans = INT_MAX;
// 走到第u个物品,已经用了c这个多的钱,使用了m这个大的容量
void dfs(int u, int c, int m) {
// 剪枝优化
if (c >= ans)
return;
if (m >= L) {
if (c < ans)
ans = c;
return;
}
// 递归出口
if (u == n + 1)
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]; // 售价和容量
dfs(1, 0, 0);
if (ans < INT_MAX)
cout << ans << endl;
else
cout << "no solution" << endl;
return 0;
}