HDU 4856 Tunnels

题目链接

题意:给定一些管道。然后管道之间走是不用时间的,陆地上有障碍。陆地上走一步花费时间1,求遍历全部管道须要的最短时间。每一个管道仅仅能走一次

思路:先BFS预处理出两两管道的距离。然后状态压缩DP求解,dp[s][i]表示状态s。停在管道i时候的最小花费

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; const int INF = 0x3f3f3f3f;
const int N = 20;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
typedef pair<int, int> pii;
#define MP(a,b) make_pair(a,b) int g[N][N], vis[N][N], n, m, dp[(1<<15)][20];
char G[N][N]; struct Pipe {
int x1, y1, x2, y2;
void read() {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
}
} p[N]; int bfs(Pipe a, Pipe b) {
queue<pii> Q;
memset(vis, -1, sizeof(vis));
Q.push(MP(a.x2, a.y2));
vis[a.x2][a.y2] = 0;
while (!Q.empty()) {
pii now = Q.front();
if (now.first == b.x1 && now.second == b.y1) return vis[now.first][now.second];
Q.pop();
for (int i = 0; i < 4; i++) {
int xx = now.first + d[i][0];
int yy = now.second + d[i][1];
if (xx <= 0 || xx > n || yy <= 0 || yy > n || vis[xx][yy] != -1 || G[xx][yy] != '.') continue;
vis[xx][yy] = vis[now.first][now.second] + 1;
Q.push(MP(xx,yy));
}
}
return -1;
} void build() {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (i == j) g[i][j] = 0;
else g[i][j] = bfs(p[i], p[j]);
}
}
} int solve() {
memset(dp, INF, sizeof(dp));
for (int i = 1; i <= m; i++)
dp[1<<(i - 1)][i] = 0;
int ans = INF;
for (int i = 0; i < (1<<m); i++) {
for (int j = 1; j <= m; j++) {
if (i&(1<<(j - 1))) {
for (int k = 1; k <= m; k++) {
if (i&(1<<(k - 1)) == 0 || g[k][j] == -1) continue;
dp[i][j] = min(dp[i^(1<<(j - 1))][k] + g[k][j], dp[i][j]);
}
}
if (i == (1<<m) - 1)
ans = min(ans, dp[i][j]);
}
}
if (ans == INF) return -1;
return ans;
} int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++)
scanf("%s", G[i] + 1);
for (int i = 1; i <= m; i++)
p[i].read();
build();
printf("%d\n", solve());
}
return 0;
}

HDU 4856 Tunnels(BFS+状压DP)的更多相关文章

  1. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  2. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  3. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU-4856 Tunnels (BFS+状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...

  5. hdu 4856 Tunnels(bfs+状态压缩)

    题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定. 解题思路:首先用bfs处理出 ...

  6. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  7. 孤岛营救问题(BFS+状压DP)

    孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...

  8. QDUOJ 来自xjy的签到题(bfs+状压dp)

    来自xjy的签到题   Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...

  9. HDU-3681-Prison Break(BFS+状压DP+二分)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

随机推荐

  1. Gzip压缩优化网站

    网站常使用GZIP压缩算法对网页内容进行压缩,然后传给浏览器,以减小数据传输量,提高响应速度.浏览器接收到GZIP压缩数据后会自动解压并正确显示.GZIP加速常用于解决网速慢的瓶颈. 压缩Filter ...

  2. Linux 程序设计学习笔记----Linux下文件类型和属性管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38318225 部分内容整理自网络,在此感谢各位大神. Linux文件类型和权限 数据表示 文件 ...

  3. svn 服务器的搭建

     SVN服务器运行模式:模式1:svn服务器单独运行  监听: 3690端口    访问: svn://IP模式2: svn 服务器+ apache   : 80 端口  访问: http://IP  ...

  4. 给centos重新安装yum的base-repo源

    转自:https://blog.csdn.net/lovemysea/article/details/79552952 如果自己的centos的系统yum源出现问题了,如何才能修复? 方式一:使用国内 ...

  5. 16.QT鼠标

    头文件 #include <QMouseEvent> #include <QStatusBar> #include <QLabel> protected: //鼠标 ...

  6. Js radio

    <input type="radio" name="sex" value="1" />男 <input type=&quo ...

  7. Failed reading log event, reconnecting to retry

    数据库版本:5.6.16 系统:CentOS 6.5 搭建数据库从库报错:160411 14:30:39 [Note] Slave I/O thread: Failed reading log eve ...

  8. 。net内存优化

    1.尽量减少和避免不必要的对象 2.优化算法和数据结构 3.采用非托管代码或者模块编写数据处理逻辑 4.NET应用程序的内存一定程度上受垃圾回收的影响.并指出,一些数据结构如List,系统会分配多余的 ...

  9. 杭电 2639 Bone Collector II【01背包第k优解】

    解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...

  10. CDR X7正版优惠,3折来袭,好礼相送,行不行动?

    意料之中的是,CorelDRAW系列软件在618期间成绩再次突破历史,成为新高.因为X7版本活动在6月15号的才上, 加之在此之前从没有过X7的活动优惠,势头之猛,可想而知,如此一来,官方预定的限量2 ...