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

49 lines
829 B
C++
Raw Permalink 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;
/*
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;
}