-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1019.cpp
More file actions
31 lines (31 loc) · 738 Bytes
/
Copy path1019.cpp
File metadata and controls
31 lines (31 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
using namespace std;
// 0/1背包问题可以用动态规划在伪多项式时间内解决。
int main()
{
int m;
cin >> m;
int dp[500][200] = {};
int data[500][2] = {};
for (int i = 0;i < m;i++)
{
int n, c;
cin >> n >> c;
for (int j = 1;j <= n;j++)
{
cin >> data[j][0] >> data[j][1];
}
for (int k = 1;k <= n;k++)
{
for (int l = 0;l <= c;l++)
{
if (l >= data[k][0])
dp[k][l] = max(dp[k - 1][l], dp[k - 1][l - data[k][0]] + data[k][1]);
else
dp[k][l] = dp[k - 1][l];
}
}
cout << dp[n][c] << endl;
}
}