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,要选当中一些数字.然 ...
随机推荐
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Greedy:Jessica's Reading Problem(POJ 3320)
Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...
- 线条围绕 div 中心转圈 效果
1. 用到知识: CSS:animate ,clipe 2.原理: 用clip 属性 将div 切边 ,会出现 切边的动态效果,然后内部的div 遮住外部的div ,流出一部分 作为边: 就是旋 ...
- ref 和 out
两者都是按地址传递的,使用后都将改变原来的数值.很多人在论坛上解释说out是按数值传递,是错误的.简单的测试后可以知道out使用也能改变数值的,所以肯定是按照地址传递的.其次:rel可以把参数的数值传 ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- qt编译mysql插件
安装MySQL,C:\Program Files (x86)\MySQL\MySQL Server 5.7,然后把include和lib文件夹拷贝到C盘,因为qmake不允许路径中有空格!!! 安装Q ...
- 模拟赛1029d2
[问题描述]祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨道上并加入原有序列中.一旦有三个或更多同色的珠子变 ...
- php数组的创建及操作
//数组的创建 //1 $usernames = array('李彦宏','周宏伟','马云','俞敏洪','李开复'); echo $usernames; //array,打印类型 echo '&l ...
- Python下安装MySQLdb
前提是你已经安装过mysql 1.从https://pypi.python.org/pypi/MySQL-python/下载MySQL-python,然后用rz命令上传到相关目录 2.用tar -zx ...