题目链接:hdu 4856 Tunnels

题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,如今有人想要体验下这M个管道,问最短须要移动的距离,起点未定。

解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std;
const int maxn = 20;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; struct point {
int x, y;
point (int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
}; struct pipe {
point s, e;
} pi[maxn]; int N, M, d[maxn][maxn];
int dp[(1<<15)+5][maxn];
char g[maxn][maxn]; int bfs (point s, point e) {
int vis[maxn][maxn];
memset(vis, -1, sizeof(vis)); queue<point> que; vis[s.x][s.y] = 0;
que.push(s); while (!que.empty()) {
point u = que.front();
que.pop(); if (u.x == e.x && u.y == e.y)
return vis[u.x][u.y]; for (int i = 0; i < 4; i++) {
int x = u.x + dir[i][0];
int y = u.y + dir[i][1]; if (x <= 0 || x > N || y <= 0 || y > N)
continue; if (vis[x][y] != -1 || g[x][y] == '#')
continue; vis[x][y] = vis[u.x][u.y] + 1;
que.push(point(x, y));
}
}
return -1;
} void init () {
for (int i = 1; i <= N; i++)
scanf("%s", g[i] + 1); memset(d, 0, sizeof(d)); for (int i = 0; i < M; i++) {
scanf("%d%d%d%d", &pi[i].s.x, &pi[i].s.y, &pi[i].e.x, &pi[i].e.y); for (int j = 0; j < i; j++) {
d[i][j] = bfs(pi[i].e, pi[j].s);
d[j][i] = bfs(pi[j].e, pi[i].s);
}
}
} int solve () {
memset(dp, INF, sizeof(dp)); for (int i = 0; i < M; i++)
dp[1<<i][i] = 0; for (int s = 0; s < (1<<M); s++) { for (int j = 0; j < M; j++) {
if (dp[s][j] == INF)
continue; for (int k = 0; k < M; k++) {
if (s&(1<<k))
continue; if (d[j][k] == -1)
continue; dp[s|(1<<k)][k] = min(dp[s|(1<<k)][k], dp[s][j] + d[j][k]);
}
}
} int ans = INF;
for (int i = 0; i < M; i++)
ans = min(ans, dp[(1<<M)-1][i]);
return ans == INF ? -1 : ans;
} int main () {
while (scanf("%d%d", &N, &M) == 2) {
init(); printf("%d\n", solve());;
}
return 0;
}

hdu 4856 Tunnels(bfs+状态压缩)的更多相关文章

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

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

  2. hdu 1429(BFS+状态压缩)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. HDU 4856 Tunnels(BFS+状压DP)

    HDU 4856 Tunnels 题目链接 题意:给定一些管道.然后管道之间走是不用时间的,陆地上有障碍.陆地上走一步花费时间1,求遍历全部管道须要的最短时间.每一个管道仅仅能走一次 思路:先BFS预 ...

  4. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  5. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  6. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  7. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  8. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  9. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 使用cacti监控服务器

    一.cacti安装 cacti需要lamp环境,因此需要首先编译安装lamp环境,安装完成后,下载cacti的zip包,解压到/www/htdocs目录下,创建cacti需要的数据库. 完成上述后,在 ...

  2. HttpAsyncClient 做并发长连接的一个实例

    HttpAsyncClient 做并发长连接的一个实例 import java.util.concurrent.CountDownLatch; import org.apache.http.HttpR ...

  3. poj3237(树链剖分)

    题目链接:http://poj.org/problem?id=3237 题目大意:指定一颗树上有3个操作: 1)询问操作,询问a点和b点之间的路径上最长的那条边的长度(即最大值): 2)取反操作,将a ...

  4. 《软件project》课程报告 —国土资源执法监察管理信息系统建模

    ***********************************************声明*************************************************** ...

  5. java使用AES加密解密 AES-128-ECB加密

    java使用AES加密解密 AES-128-ECB加密 import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; impo ...

  6. ios7开发者必知

    如果你想为iOS 设备开发app,你需要知道如何与软件交互,如何设计,你还要知道苹果独特的开发理念和开发工具.真正的能力还需要成功地从其他行业领域借鉴核心概念.最后把所有这些东西糅合进你的信息库中, ...

  7. 使用OGG&quot;Loading data from file to Replicat&quot;的方法应该注意的问题:replicat进程是前台进程

    使用OGG的 "Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程 因此.最好是在vncserver中调用该 ...

  8. 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)

    接口隔离原则: 使用多个专门的接口比使用单一的总接口要好. 一个类对另外一个类的依赖性应当是建立在最小的接口上的. 一个接口代表一个角色,不应当将不同的角色都交给一个接口.没有关系的接口合并在一起,形 ...

  9. wscript:329: error: Could not autodetect OpenSSL support. Make sure OpenSSL development packages are

    安装node错: wscript:329: error: Could not autodetect OpenSSL support. Make sure OpenSSL development pac ...

  10. Qt学习一门:直接使用QT具

    今天,通过直接使用QT一些工具来编写命令行程序.你可以看到一种Qt更一般的用法. 内容很easy,输出电流日期. 首先,用一个QDate分类,可以使用QDate类的静态方法currentDate为了得 ...