LightOJ 1037 - Agent 47(状压DP)
来源:互联网 发布:数据恢复免费破解版 编辑:程序博客网 时间:2024/05/07 12:33
题意:特工杀人,起始手枪一枪伤害为1。杀谁得到谁的武器。并且已知每个武器对每个敌人一枪的伤害。问最少多少枪干掉所有人。
思路:最多就15个人,很容易想到状压。a[s][i]表示,已杀过的人的状态为s,对第i个敌人一枪可造成的最大伤害。dp[s]表示杀过敌人状态为s最少要多少枪。状态表示一想到,剩下的就很水了。预处理一下a数组,再跑遍记忆化。Done.
#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <string>#include <queue>#include <stack>#include <cmath>#include <set>#include <map>using namespace std;typedef long long LL;#define mem(a, n) memset(a, n, sizeof(a))#define ALL(v) v.begin(), v.end()#define si(a) scanf("%d", &a)#define sii(a, b) scanf("%d%d", &a, &b)#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)#define pb push_back#define eps 1e-8const int inf = 0x3f3f3f3f, N = 6e4 + 5, MOD = 1e9 + 7;int T, cas = 0;int n, m, Lim;int dp[N], a[N][20];int hp[20], maz[20][20];int dfs(int s) { if(~dp[s]) return dp[s]; if(s == 0) return dp[s] = 0; dp[s] = inf; for(int i = 0; i < n; i ++) { if((1 << i) & s) { int newState = s - (1 << i); dp[s] = min(dp[s], dfs(newState) + (hp[i] + a[newState][i] - 1) / a[newState][i]); } } return dp[s];}void init() { Lim = (1 << n) - 1; for(int s = 0; s <= Lim; s ++) { for(int i = 0; i < n; i ++) { if((1 << i) & s) { int newState = s - (1 << i); for(int j = 0; j < n; j ++) { a[s][j] = max(a[newState][j], maz[i][j]); } } } }}int main(){#ifdef LOCAL freopen("/Users/apple/input.txt", "r", stdin);// freopen("/Users/apple/out.txt", "w", stdout);#endif si(T); while(T --) { si(n); mem(dp, -1), mem(a, 0); for(int i = 0; i < n; i ++) { si(hp[i]); a[0][i] = 1; } for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { scanf("%1d", &maz[i][j]); } } init(); printf("Case %d: %d\n", ++ cas, dfs(Lim)); } return 0;}
0 0
- lightoj 1037 - Agent 47 (状压DP)
- LightOJ - 1037 Agent 47(状压DP)
- LightOJ 1037 - Agent 47 (状压dp)
- LightOJ 1037 - Agent 47(状压DP)
- lightoj 1037 - Agent 47 状压DP
- lightoj 1037 - Agent 47 【状压dp】
- LightOJ 1037 Agent 47(状压DP)
- Lightoj 1037 Agent 47(状压DP)
- Lightoj 1037 - Agent 47 详解(状压DP)
- LightOJ 1037 - Agent 47(dp)
- lightoj 1037 Agent 47
- LightOJ 1037 Agent 47
- lightoj 1037 - Agent 47
- lightoj 1037 Agent-47(状态压缩dp~)
- 1037 - Agent 47(状压DP)
- Light OJ 1037 Agent 47 (状压DP)
- Light-1037 Agent 47 DP
- Light oj 1037 - Agent 47(状压dp)
- php 将session记录到redis中
- Linux学习笔记--1.简介
- socket setSoTimeOut
- Qt 实现的拷贝文件夹的函数
- UIView 设置圆角和边框备忘
- LightOJ 1037 - Agent 47(状压DP)
- 在布局中加入gif动图
- jquery给table动态添加tr
- C#中的委托与事件
- java 正则 抠出内容并用值进行替换
- 类方法isAssignableFrom、instanceof和asSubclass的区别及实例
- 实习整理(十三)
- NSUserDefaults简介和简单使用
- java.lang.NoSuchFieldError: deferredExpression