题目链接: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. Android开发系列(二十二):AdapterViewFlipper的功能和使用方法

    AdapterViewFlipper继承了AdapterViewAnimator,它会显示一个View组件,能够通过showPrevious()和showNext()方法控制组件显示上一个.下一个组件 ...

  2. nginx 301跳转到带www域名方法rewrite(转)

    首先一.得在你的域名管理里面定义 test.com和www.test.com指向你的主机ip地址,我们可以使用nslookup命令测试:直接输入 nslookup test.com和nslookup ...

  3. hdu1394(线段树求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...

  4. Home · chineking/cola Wiki

    Home · chineking/cola Wiki Home Cola Cola是一个分布式的爬虫框架,用户只需编写几个特定的函数,而无需关注分布式运行的细节.任务会自动分配到多台机器上,整个过程对 ...

  5. Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程

    Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程 引言:原本机子上已经装好了win7+Ubuntu Kylin 由win7引导,而不是Ubuntu的grub引导的双系统(安装 ...

  6. 让Linux开机运行命令

    开机的时候需要linux 自动执行命令很简单 只需要把要执行的命令输入操作系统启动的时候要加载的文件里面就行了,一般写在 /etc/rc.local里面 #vim /etc/rc.local 按o键  ...

  7. 字符串拼接 拆分 NameValueCollection qscoll = HttpUtility.ParseQueryString(result)

    string result = "sms&stat=100&message=发送成功"; string d = HttpUtility.ParseQueryStri ...

  8. M3U8格式解说及实际应用分析

    M3U8有啥优点 ? 网上搜索了一下,大家众说纷纭,个人理解主要是能够做多码率的适配,依据网络带宽,client会选择一个适合自己码率的文件进行播放,保证视频流的流畅. 在IOS device和mac ...

  9. 你不知道的Eclipse的用法:使用MAT分析Android的内存

    如果使用DDMS确实发现了我们程序中存在内存泄露,那如何定位到具体出现问题的代码片段,最终找到问题所在呢?如果从头到尾分析代码逻辑,那肯定会把人逼疯,特别是在维护别人写的代码的时候.这里介绍一个极好的 ...

  10. (hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

    题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...