hdu 4568 Hunter(spfa预处理 + 状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568
思路:首先spfa预处理出每对宝藏之间的最短距离以及宝藏到边界的最短距离,然后dp[state][u]表示当前在点u,状态为state的最短距离,然后更新就行。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int MAX_N = (233);
const int inf = 0x3f3f3f3f;
int N, M, n, g[MAX_N][MAX_N]; struct Point {
int x, y;
} point[14]; int d[14][14], dd[14], dp[(1 << 14) + 4][14]; //d[i][j]表示宝藏i到宝藏j之间的最短距离,dd[i]表示宝藏i到边界的最短距离
bool vis[MAX_N][MAX_N]; struct Node {
int x, y, step;
Node () {}
Node (int _x, int _y, int _step) : x(_x), y(_y), step(_step) {}
}; int dist[MAX_N][MAX_N], dir[4][2] = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}
};
void spfa(int index)
{
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) dist[i][j] = inf, vis[i][j] = false;
}
queue<pair<int, int > > que;
que.push(make_pair(point[index].x, point[index].y));
dist[point[index].x][point[index].y] = 0; while (!que.empty()) {
pair<int, int > p = que.front();
que.pop(); vis[p.first][p.second] = false; for (int i = 0; i < 4; ++i) {
int x = p.first + dir[i][0];
int y = p.second + dir[i][1]; if (g[x][y] == -1) continue; if (x < 0 || x >= N || y < 0 || y >= M) {
dd[index] = min(dd[index], dist[p.first][p.second] + g[point[index].x][point[index].y]);
continue;
} if (dist[p.first][p.second] + g[x][y] < dist[x][y]) {
dist[x][y] = dist[p.first][p.second] + g[x][y];
if (!vis[x][y]) {
vis[x][y] = true;
que.push(make_pair(x, y));
}
}
}
}
} int main()
{
int Cas;
scanf("%d", &Cas);
while (Cas--) {
scanf("%d %d", &N, &M);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) scanf("%d", &g[i][j]);
}
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d %d", &point[i].x, &point[i].y); for (int i = 0; i < (1 << n); ++i) {
for (int j = 0; j < n; ++j) dp[i][j] = inf;
} memset(dd, 0x3f, sizeof(dd));
for (int i = 0; i < n; ++i) {
spfa(i);
for (int j = 0; j < n; ++j) {
if (i == j) d[i][j] = 0;
else d[i][j] = dist[point[j].x][point[j].y];
} dp[1 << i][i] = dd[i];
} for (int s = 0; s < (1 << n); ++s) {
for (int i = 0; i < n; ++i) {
if ( dp[s][i] != inf && ((1 << i) & s)) {
for (int j = 0; j < n; ++j) if (i != j && (!((1 << j) &s))) {
dp[s | (1 << j)][j] = min(dp[s | (1 << j)][j], dp[s][i] + d[i][j]);
}
}
}
} int ans = inf;
for (int i = 0; i < n; ++i) {
ans = min(ans, dp[(1 << n) - 1][i] + dd[i] - g[point[i].x][point[i].y]);
} printf("%d\n", ans); }
return 0;
}
hdu 4568 Hunter(spfa预处理 + 状压dp)的更多相关文章
- HDU 4568 Hunter 最短路+状压DP
题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...
- loj 1316(spfa预处理+状压dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...
- HDU 4899 Hero meet devil (状压DP, DP预处理)
题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...
- HDU 6149 Valley Numer II 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU - 4284 Travel(floyd+状压dp)
Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
随机推荐
- poj 1011
http://poj.org/problem?id=1011 这是一道POJ的搜索的题目,最开始确实难以理解,但做过一些搜索的题目后,也没那么难了. 大概题意就是,现在有N根木头,要拼成若干根木头,并 ...
- 4.js模式-发布-订阅模式
1. 发布-订阅模式 var observe = (function(){ var events = {}, listen, trigger, remmove; listen = function(k ...
- 多国语言文档识别 ABBYY FineReader Corporate v12.0.101.388.7z 绿色破解版
ABBYY 是一家俄罗斯软件公司,在文档识别,数据捕获和语言技术的开发中居世界领先地位.其获奖产品 FineReader OCR 软件可以把静态纸文件和 PDF 文件转换成可管理的电子数据,可以大大节 ...
- Can't bind to local 8700 for debugger报错和解决
[2016-02-15 22:37:17 - ddms] Can't bind to local 8700 for debugger报错和解决 1.打开studio monitor是出错: Can't ...
- nyoj756_重建二叉树_先序遍历
重建二叉树 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数 ...
- H5 前端页面适配响应式
辞职有半个月了,面试了几家公司,还在挣扎中.... 不废话,H5页面适配成响应式,可以用百分比或者rem. rem是相对于html根元素的单位,可以根据根元素的大小做出等比缩放, 通常,假如设置,ht ...
- 【python】词法语法解析模块ply
官方手册:http://www.dabeaz.com/ply/ply.html 以下例子都来自官方手册: 以四则运算为例: x = 3 + 42 * (s - t) 词法分析: 需要将其分解为: 'x ...
- ArtDialog简单使用示例
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- [Android Pro] APK
svn updatesvn status ls -alsvn log --limit 8 > RELEASE_NOTE.txt cat RELEASE_NOTE.txt chmod a+x gr ...
- hdu2108(判断凸多边形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2108 题意: 给出一个多边形的所有顶点,判断是不是凸多边形: 思路: 判断凸多边形的方法比较多,如:若 ...