hiho 1270 建造基地 dp 完全背包 模拟
2016-03-07 11:10
92 查看
题目
题目链接:http://hihocoder.com/problemset/problem/1270题目来源:hiho上的比赛
简要题意:建造基地,有代价和贡献,基地升级贡献要除tt求建造的最小代价。
题解
题目很长,变量非常多,搞得人很不舒服,但是想了想就能发现就是个背包变形。首先可以去判断是否可以建造出来,就是看能否对nn级基地产生贡献,一个小剪枝。
然后就是去做nn轮完全背包,记录答案,每轮更新贡献值。
做完全背包的时候需要有些小变化,上界要变动,把超出的转移到kk。
代码也不短,如果题意没这么复杂就更好了。
代码
#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; }
相关文章推荐
- leetcode:Binary Tree Inorder Traversal 【Java】
- PhpStorm 快捷键大全
- Mechanize对WEB的操作
- MySQL root用户被删除解决办法
- Java与设计模式-责任链模式
- 《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
- Delphi word 颜色
- 在Windows的Wamp环境下安装Composer
- 使用imageIO获取和修改图片的exif信息
- Reason: no suitable image found. Did find:
- 华为oj 记票统计
- XListView使用
- delphi 操作 word
- poj 3253 Fence Repair(优先队列+哈夫曼树)
- kafka使用getOffsetsBefore()获取获取offset异常分析
- Java WebService 简单实例
- [Unity优化]UGUI图集的使用
- 读者与写者的问题 (
- 在C#的类库中添加web service引用
- [从头学数学] 第125节 不等式与不等式组 小结与复习题