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,要选当中一些数字.然 ...
随机推荐
- MySQL ODBC for Linux
参考自http://blog.csdn.net/allens_zhou/article/details/8575400 centos7 64bit [IP:192.168.0.100] yum ins ...
- strcpy vs memcpy
[本文连接] http://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html [分析] strcpy和memcpy都是标准C库函数,它们有下面的特点 ...
- iOS NSObject 的 isa 属性的类型 Class
以前对NSObject的isa属性也知道点,但是了解不深,今天看了这篇博文,感觉很好,总结一下: http://chun.tips/blog/2014/11/05/bao-gen-wen-di-obj ...
- ACM/ICPC 之 简单DP-记忆化搜索与递推(POJ1088-滑雪)
递推型DP 将每个滑雪点都看作起点,从最低点开始逐个由四周递推出到达此点的最长路径的长度,由该点记下. 理论上,也可以将每一点都看作终点,由最高点开始计数,有兴趣可以试试. //经典DP-由高向低海拔 ...
- 9.SpringMVC和json结合传递参数
input的值一定要用.attribute来取值.val( )只能用可以看看开源社区jQuery的ajax请求.html():读取和修改一个元素的HTML内容,详情.html():.text():读取 ...
- WIZnet官方网盘
之前使用过 WIZnet 的TCP/IP 解决方案,资源较少, 偶然发现此网盘,不敢独享,访问 请戳此处.
- LAMP 之 mysql 安装
搞了成日 = = 呢个野.... 大部分东西写在 印象笔记 中....不过呢个野特别繁琐,所以记录落黎(小白一枚,大家见谅) 总结下,唔系好容易唔记得 >W< (可能唔会甘完整,我将我自认 ...
- DB2环境设置
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.级别对应 • Environment variables at the operating system l ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
- 【XLL 文档翻译】【第2部分】C API 回调函数 Excel4, Excel12
Excel4 和 Excel12 函数使得 DLL 可以调用 Excel 工作表函数.宏表函数.命令.XLL特定函数或命令.最近的一些 Excel 版本都支持 Excel12 函数.这两个函数支持下面 ...