You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
/**
*9.
1
01
2
01
* @return
*/
#define maxV 1000
#define maxN 100
int main(void) {
int cases, n, v, ci[maxN], wi;
int f[maxV];
bool g[maxN][maxV]; //g[i][v]=0 表示没放i时的f(i, v)较大,
//g[i][v]=1 表示放进i时的f(i, v)较大
freopen("../9.txt", "r", stdin);
cin >> cases;
while (cases--) {
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
cin >> n >> v;
for (int i = 0; i < n; i++) {
cin >> ci[i] >> wi;
for (int j = v; j >= 0; j--) {
if (j >= ci[i]) {
if (f[j - ci[i]] + wi > f[j]) {
f[j] = f[j - ci[i]] + wi;
g[i][j] = 1;
}
}
}
}
int i = n - 1, j = v;
while (i >= 0) {
if (g[i][j] == 1) {
cout << "选了" << i << endl;
j -= ci[i];
} else {
cout << "没选" << i << endl;
}
i--;
}
cout << endl;
}
}