hiho 1270 建造基地 dp 完全背包 模拟

题目

题目链接:http://hihocoder.com/problemset/problem/1270

题目来源:hiho上的比赛

简要题意:建造基地,有代价和贡献,基地升级贡献要除 t 求建造的最小代价。

题解

题目很长,变量非常多,搞得人很不舒服,但是想了想就能发现就是个背包变形。

首先可以去判断是否可以建造出来,就是看能否对n级基地产生贡献,一个小剪枝。

然后就是去做 n 轮完全背包,记录答案,每轮更新贡献值。

做完全背包的时候需要有些小变化,上界要变动,把超出的转移到k

代码也不短,如果题意没这么复杂就更好了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int M = 105;
const int MX = 1e4+5;
const LL INF = 0x3f3f3f3f3f3f3f3f;

LL a[M];
LL b[M];

// 判断是否能够建造完
// 对于n级建筑有一个材料可以产生贡献就能建造完
bool ck(int n, int m, int k, int t) {
    for (int i = 0; i < m; i++) {
        LL temp = b[i];
        for (int j = 1; j < n; j++) {
            temp /= t;
        }
        if  (temp) return true;
    }
    return false;
}

LL dp[MX];

// 做完全背包,注意上界应该进行变化
// 超过k的转移到k
void completePack(int m, int k) {
    for (int i = 0; i < m; i++) {
        if (!b[i]) continue;
        for (int j = 0; j < k; j++) {
            int nxt = min(int(j + b[i]), k);
            dp[nxt] = min(dp[nxt], dp[j] + a[i]);
        }
    }
}

LL solve(int n, int m, int k, int t) {
    LL ans = 0;
    for (int i = 0; i < n; i++) {
        // 做背包更新答案
        memset(dp, INF, sizeof dp);
        dp[0] = 0;
        completePack(m, k);
        ans += dp[k];
        // 更新贡献值
        for (int j = 0; j < m; j++) {
            b[j] /= t;
        }
    }
    return ans;
}

int main() {
    int n, m, k, t, tt;
    cin >> tt;
    while (tt--) {
        cin >> n >> m >> k >> t;
        for (int i = 0; i < m; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> b[i];
        }
        if (!ck(n, m, k, t)) {
            cout << "No Answer" << endl;
        } else {
            cout << solve(n, m, k, t) << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值