47 lines
973 B
C++
47 lines
973 B
C++
|
|
#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;
|
|||
|
|
}
|