HDU 4856 Tunnels

题目链接

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

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

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int INF = 0x3f3f3f3f;
  9. const int N = 20;
  10. const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  11. typedef pair<int, int> pii;
  12. #define MP(a,b) make_pair(a,b)
  13.  
  14. int g[N][N], vis[N][N], n, m, dp[(1<<15)][20];
  15. char G[N][N];
  16.  
  17. struct Pipe {
  18. int x1, y1, x2, y2;
  19. void read() {
  20. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  21. }
  22. } p[N];
  23.  
  24. int bfs(Pipe a, Pipe b) {
  25. queue<pii> Q;
  26. memset(vis, -1, sizeof(vis));
  27. Q.push(MP(a.x2, a.y2));
  28. vis[a.x2][a.y2] = 0;
  29. while (!Q.empty()) {
  30. pii now = Q.front();
  31. if (now.first == b.x1 && now.second == b.y1) return vis[now.first][now.second];
  32. Q.pop();
  33. for (int i = 0; i < 4; i++) {
  34. int xx = now.first + d[i][0];
  35. int yy = now.second + d[i][1];
  36. if (xx <= 0 || xx > n || yy <= 0 || yy > n || vis[xx][yy] != -1 || G[xx][yy] != '.') continue;
  37. vis[xx][yy] = vis[now.first][now.second] + 1;
  38. Q.push(MP(xx,yy));
  39. }
  40. }
  41. return -1;
  42. }
  43.  
  44. void build() {
  45. for (int i = 1; i <= m; i++) {
  46. for (int j = 1; j <= m; j++) {
  47. if (i == j) g[i][j] = 0;
  48. else g[i][j] = bfs(p[i], p[j]);
  49. }
  50. }
  51. }
  52.  
  53. int solve() {
  54. memset(dp, INF, sizeof(dp));
  55. for (int i = 1; i <= m; i++)
  56. dp[1<<(i - 1)][i] = 0;
  57. int ans = INF;
  58. for (int i = 0; i < (1<<m); i++) {
  59. for (int j = 1; j <= m; j++) {
  60. if (i&(1<<(j - 1))) {
  61. for (int k = 1; k <= m; k++) {
  62. if (i&(1<<(k - 1)) == 0 || g[k][j] == -1) continue;
  63. dp[i][j] = min(dp[i^(1<<(j - 1))][k] + g[k][j], dp[i][j]);
  64. }
  65. }
  66. if (i == (1<<m) - 1)
  67. ans = min(ans, dp[i][j]);
  68. }
  69. }
  70. if (ans == INF) return -1;
  71. return ans;
  72. }
  73.  
  74. int main() {
  75. while (~scanf("%d%d", &n, &m)) {
  76. for (int i = 1; i <= n; i++)
  77. scanf("%s", G[i] + 1);
  78. for (int i = 1; i <= m; i++)
  79. p[i].read();
  80. build();
  81. printf("%d\n", solve());
  82. }
  83. return 0;
  84. }

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. Django -&gt; debug模式下的静态文件服务(/media/)

    正式公布django项目的时候,假设存在静态文件(通常会统一放在名称为media或static的文件夹下),则须要建立url到文件系统的映射,比如.使用nginx的时候我们须要进行这种配置. # Dj ...

  2. Hadoop问题记录:Wrong FS: hdfs://hp5-249:9000/, expected: file:///

    一般在对文件操作的时候可能出现这个问题,可能是打开文件的时候出错,也可能是对文件夹进行遍历的时候出问题. 出现这样的问题通常是在eclipse中执行hadoop的时候出现,直接切换到shell下发送命 ...

  3. hdu(2846)Repository

    Problem Description When you go shopping, you can search in repository for avalible merchandises by ...

  4. webservice为什么不能用List参数,而只能用数组代替,我想是否因为List没有具体的类型信息,但用泛型的List(如:List<customer>)为什么也不行。如果用作参数的类中含有List<T>字段该如何处理?webservice参数是否支持

    转自:https://social.microsoft.com/Forums/zh-CN/aded4301-b5f1-4aa6-aa46-16c46a60d05e/webservice20026201 ...

  5. Flask上下文管理机制

    前引 在了解flask上下文管理机制之前,先来一波必知必会的知识点. 面向对象双下方法 首先,先来聊一聊面向对象中的一些特殊的双下划线方法,比如__call__.__getattr__系列.__get ...

  6. Redis学习笔记(二) Redis 数据类型

    Redis 支持五种数据类型:string(字符串).list(列表).hash(哈希).set(集合)和 zset(有序集合),接下来我们讲解分别讲解一下这五种类型的的使用. String(字符串) ...

  7. BZOJ 4184 线段树+高斯消元

    思路: 线段树表示的是时间 每回最多log个段 区间覆盖 一直到叶子 的线性基 xor 一下 就是答案 一开始没有思路 看了这篇题解 豁然开朗 http://www.cnblogs.com/joyou ...

  8. ASP.net 中 OutputCache 指令各个参数的作用

    使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...

  9. rmi 工作原理

    rmi 工作原理 (转) 工作SocketJava应用服务器网络应用  RMI(Remote Method Invocation,远程方法调用)是Java的一组拥护开发分布式应用程序的API.RMI使 ...

  10. thinkphp5 模板中截取中文字符串

    TP5模板页截取中文字符串 {$vo.task_detail|mb_substr=###,0,15,'utf-8'}