acahv @ 2022-05-12 21:15:06
思路是用一个额外的数组记录每个件的附件情况,同时用一个bool型判断这个数组是不是主件,如果他的主件编号不是0就说明他是别人的附属 但是只有30分
测试数据:
4500 12
100 3 0
400 5 0
300 5 0
1400 2 0
500 2 0
800 2 4
1400 5 4
300 5 0
1400 3 8
500 2 0
1800 4 0
440 5 10
正确答案:16700
我的答案:18700
#include<bits/stdc++.h>
using namespace std;
int n, m;
int item[66][2];
int cnt[66];
int value[66], weight[66];
bool is_main[66];
int f[32010];
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int d;
cin >> weight[i] >> value[i] >> d;
if (d != 0)is_main[i] = 1;
item[d][cnt[d]++] = i;
}
for (int i = 1; i <= m; i++)
{
for (int j = n; j >= weight[i] && !is_main[i]; j--)
{
f[j] = max(f[j], f[j - weight[i]] + weight[i] * value[i]);
if (j >= weight[i] + weight[item[i][0]])
{
f[j] = max(f[j], f[j - weight[i] - weight[item[i][0]]] + weight[i] * value[i] + weight[item[i][0]] * value[item[i][0]]);
}
if (j >= weight[i] + weight[item[i][1]])
{
f[j] = max(f[j], f[j - weight[i] - weight[item[i][1]]] + weight[i] * value[i] + weight[item[i][1]] * value[item[i][1]]);
}
if (j >= weight[i] + weight[item[i][1]] + weight[item[i][0]])
{
f[j] = max(f[j], f[j - weight[i] - weight[item[i][1]] - weight[item[i][0]]] + weight[i] * value[i] + weight[item[i][1]] * value[item[i][1]] + weight[item[i][0]] * value[item[i][0]]);
}
}
}
cout << f[n];
return 0;
}