[1507] Luo Gu NASA's food program

Topic background

NASA (National Aeronautics and Space Administration) because of the space shuttle heat shield tiles and other security technology issue has been a big headache, so under pressure from all sides to terminate the history of the space shuttle, but such things will not happen in the future, who also We can not guarantee that, in the face of this kind of space problem, the solution may only allow astronauts to repair the warehouse, but many times maintenance can consume large amounts of energy astronauts, NASA and therefore he wanted to design a food plan, and let the volume under conditions of limited load-bearing multi-load some high-calorie foods.

Title Description

Finite volume of the space shuttle, of course, if the load is too heavy items, fuel will waste a lot of money, each food has its own volume, quality, and calories contained in telling you the maximum volume and quality of the case, please output It can reach a maximum of calories contained in the food program, of course, each food can only be used once.

Input and output formats

Input format:
The first line of the maximum volume of the two numbers (<400) and a maximum mass (<400)

Total food a second line number N (<50).

Third row - 3 + N rows of

Each row number three volumes (<400) mass (<400) contained Calories (<500)

Output format:
a maximum achievable number of calories (int within range)

Sample input and output

Input Sample # 1: Copy
320. 350
. 4
160. 40 120
240 80 110
220 70 310
40 400 220
Output Sample # 1: Copy
550


Solution: knapsack problem is a simple dp ah, starting from 1 to enumerate
every food, f [j] [k] represents the volume of j, k highest quality
calories.

code show as below

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;
int v[55],m[55],kll[55];
int V,M,n,f[402][402];
int main(){
    scanf("%d %d %d",&V,&M,&n);
    for(int i=1;i<=n;i++)
        scanf("%d %d %d",&v[i],&m[i],&kll[i]);
    for(int i=1;i<=n;i++)
        for(int j=V;j>=v[i];j--)
            for(int k=M;k>=m[i];k--)
                f[j][k]=max(f[j][k],f[j-v[i]][k-m[i]]+kll[i]);
    cout<<f[V][M];
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11129364.html