其他分享
首页 > 其他分享> > 一本通 1271:【例9.15】潜水员

一本通 1271:【例9.15】潜水员

作者:互联网

潜水员

二维费用背包


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky   
//
#define M 1000
#define INF 0x3f3f3f3f
int f[M][M];
int a[M], b[M], c[M];
int n, m, k;
int main() {
    scanf("%d%d", &m, &n);
    scanf("%d", &k);
    for(int i = 1; i <= k; i++) scanf("%d%d%d", &a[i], &b[i], &c[i]);
    memset(f, INF, sizeof(f));
    f[0][0] = 0;
    for(int i = 1; i <= k; i++)
        for(int j = m; j >= 0; j--) 
            for(int l = n; l >= 0; l--) {
                int x, y;
                x = j + a[i], y = l + b[i];
                x = x > m ? m : x;
                y = y > n ? n : y;
                f[x][y] = min(f[x][y], f[j][l] + c[i]);
            }
    printf("%d\n", f[m][n]);
    return 0;
}

标签:潜水员,int,1271,scanf,9.15,include,define
来源: https://www.cnblogs.com/Benjamin-cpp/p/10994496.html