流浪西邮之寻找火石碎片

题面:

现在你有v1块钱,v2积分,能免费(免积分)收集k个火石碎片,现在总共有n个火石碎片,每个碎片需要的钱a或者积分b,碎片的能量为val。我们希望收集火石碎片,使能量的总和尽可能大,问你skyer_hxx最多可以拿到能量总和的最大值是多少?

思路:

0/1背包

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3  
 4 int f[105][105][105][10];
 5 int a[105], b[105], v[105];
 6  
 7 int n,ans, v1, v2, k;
 8 int main()
 9 {
10     while (~scanf("%d%d%d%d", &n, &v1, &v2, &k))
11     {
12         for (int i = 1; i <= n; i++)
13         {
14             scanf("%d%d%d", &a[i], &b[i], &v[i]);
15         }
16         ans = 0;
17         memset(f,0,sizeof(f));
18         for (int i = 1; i <= n; i++)
19             for (int x = 0; x <= v1; x++)
20                 for (int y = 0; y <= v2; y++)
21                     for (int j = 0; j <= k; j++)
22                     {
23                         f[i][x][y][j] = f[i - 1][x][y][j];
24                         if (x >= a[i])
25        f[i][x][y][j] = max(f[i][x][y][j], f[i - 1][x - a[i]][y][j] + v[i]);
26                         if (y >= b[i])
27         f[i][x][y][j] = max(f[i][x][y][j], f[i - 1][x][y - b[i]][j] + v[i]);
28                         if (j > 0)
29         f[i][x][y][j] = max(f[i][x][y][j], f[i - 1][x][y][j - 1] + v[i]);
30                         ans = max(f[i][x][y][j], ans);
31                     }
32         printf("%d\n", ans);
33     }
34 }
View Code

 

猜你喜欢

转载自www.cnblogs.com/Accpted/p/11185448.html
0条评论
添加一条新回复