Tunnels

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of curiosity and he wants to visit all of the secret tunnels beneath the city. To travel in a tunnel, he has to walk to the entrance of the tunnel and go out from the exit after a fabulous visit. He can choose where he starts and he will travel each of the tunnels once and only once. Now he wants to know, how long it will take him to visit all the tunnels (excluding the time when he is in the tunnels).
 
Input
The input contains mutiple testcases. Please process till EOF.
For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.
The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”.
Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) in the map are both empty grid.
 
Output
For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels.
If it is impossible for Bob to visit all the tunnels, output -1.
 
Sample Input
5 4
....#
...#.
.....
.....
.....
2 3 1 4
1 2 3 5
2 3 3 1
5 4 2 1
 
Sample Output
7
 
Source

题意:一个图,#不可达,m条单向通道,点间花费1时间,通道起点到终点不花费时间,求最少花费时间;

思路:状态压缩dp,dp[i][j] i表示已经走过哪些通道,j表示最后的走的那条通道是哪条。

   dp[i][j]=dp[i-(1<<(j-1)][k] +dis(k,j)  dis(k,j)表示第k条通道的终点到第j条通道的起点距离;

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<cstring>
  10. #include<vector>
  11. #include<list>
  12. #include<set>
  13. #include<map>
  14. #include<bitset>
  15. #include<time.h>
  16. using namespace std;
  17. #define LL long long
  18. #define pi (4*atan(1.0))
  19. #define eps 1e-8
  20. #define bug(x) cout<<"bug"<<x<<endl;
  21. const int N=+,M=1e6+,inf=1e9+,MOD=1e9+;
  22. const LL INF=1e18+,mod=1e9+;
  23.  
  24. int n,m,vis[N][N];
  25. char a[N][N];
  26. struct is
  27. {
  28. int s,t,e,d;
  29. } q[N];
  30. int check(int x,int y)
  31. {
  32. if(x<=||x>n||y<=||y>n)return ;
  33. return ;
  34. }
  35. int dis[N][N][N][N];
  36. int xx[]= {,,-,};
  37. int yy[]= {,,,-};
  38. void bfs(int s,int t)
  39. {
  40. queue<pair<int,int> >q;
  41. q.push(make_pair(s,t));
  42. memset(vis,,sizeof(vis));
  43. vis[s][t]=;dis[s][t][s][t]=;
  44. while(!q.empty())
  45. {
  46. pair<int,int> p=q.front();
  47. q.pop();
  48. for(int i=; i<; i++)
  49. {
  50. int x=p.first+xx[i];
  51. int y=p.second+yy[i];
  52. if(check(x,y)&&!vis[x][y]&&a[x][y]=='.')
  53. {
  54. vis[x][y]=;
  55. dis[s][t][x][y]=dis[s][t][p.first][p.second]+;
  56. q.push(make_pair(x,y));
  57. }
  58. }
  59. }
  60. }
  61. int dp[][N];
  62. int main()
  63. {
  64. while(~scanf("%d%d",&n,&m))
  65. {
  66. memset(dis,-,sizeof(dis));
  67. for(int i=; i<=n; i++)
  68. scanf("%s",a[i]+);
  69. for(int i=; i<=m; i++)
  70. scanf("%d%d%d%d",&q[i].s,&q[i].t,&q[i].e,&q[i].d);
  71. for(int i=;i<=n;i++)for(int j=;j<=n;j++)bfs(i,j);
  72. memset(dp,-,sizeof(dp));
  73. for(int i=;i<=(<<m)-;i++)
  74. {
  75. for(int j=;j<=m;j++)
  76. {
  77. if((<<(j-))&i)
  78. {
  79. int now=i-(<<(j-));
  80. if(!now)
  81. {
  82. dp[i][j]=;
  83. continue;
  84. }
  85. for(int k=;k<=m;k++)
  86. {
  87. if(now&(<<(k-)))
  88. {
  89. if(dp[now][k]!=-&&dis[q[k].e][q[k].d][q[j].s][q[j].t]!=-)
  90. {
  91. int temp=dp[i][j];
  92. dp[i][j]=dp[now][k]+dis[q[k].e][q[k].d][q[j].s][q[j].t];
  93. if(temp!=-)dp[i][j]=min(dp[i][j],temp);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. int ans=inf;
  101. for(int i=;i<=m;i++)
  102. if(dp[(<<m)-][i]!=-)ans=min(ans,dp[(<<m)-][i]);
  103. if(ans!=inf)printf("%d\n",ans);
  104. else printf("-1\n");
  105. }
  106. return ;
  107. }

hdu 4856 Tunnels 状态压缩dp的更多相关文章

  1. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  2. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  3. HDU 3001【状态压缩DP】

    题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数 ...

  4. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  5. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  6. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  7. HDU 2167 Pebbles 状态压缩dp

    Pebbles Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

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

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

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

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

随机推荐

  1. db2一、查询

    1.查询单条数据( fetch 放在最后) select * from x where 1=1 order by id fetch first 1 rows only

  2. 网页静态化解决方案-Freemarker demo+语法

    1.网页静态化技术Freemarker 1.1为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说 ...

  3. Pocket Gem OA: Log Parser

    time a given player spends actually connected to the network. We keep console logs of various game s ...

  4. PYTHON装饰器用法及演变

    '''开放封闭原则: 软件一旦上线之后就应该满足开放封闭原则 具体就是指对修改是封闭的,对扩展是开放的装饰器:什么是装饰器:装饰就是修饰,器指的是工具装饰器本省可以是任意可调用的对象被装饰的对象也可以 ...

  5. java23种设计模式之: 策略模式,观察者模式

    策略模式  --老司机开车,但是他今天想到路虎,明天想开奔驰...针对他不同的需求,来产生不同的应对策略    策略类是一个接口,定义了一个大概的方法,而实现具体的策略则是由实现类完成的,这样的目的是 ...

  6. C# 正则表达式提取字符串中括号里的值

    version = Regex.Replace(str, @"(.*\()(.*)(\).*)", "$2"); //小括号() Regex rgx = new ...

  7. 微信网页授权获取用户openid及用户信息

    $code = $_GET["code"];//获取code $appid=“xxxx”;//公众号appid $APPSECRET="xxx";//公众号ap ...

  8. springmvc配置之mvc:annotation-driven

    为了简化springmvc配置,spring同时引入了mvc namespace, 配置了 <mvc:annotation-driven/> spring会默认注册a RequestMap ...

  9. C博客作业03--函数

    1. 本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 这几周学习了函数,题目还是原样只是多了种做题的方法.一开始看书感觉声明,定义啊,还有全局变量那些,文绉 ...

  10. Linux的邮件服务器配置

    一.邮件服务简介 1.电子邮件服务是Interne上最基本的服务之一,进入互联网的用户不需要任何纸张就可以方便地使用电子邮件来收发邮件 2.Internet上的电子邮件服务都是基于客户/服务器模式的 ...