HDU - 4856 Tunnels (预处理+状压dp)

【hud链接】
【vj链接】


题目

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

题目大意和解题思路

输入第一行为n m (表示在n*n的地图上有m条隧道(单向));
接下来n*n的地图,其中’#’表示不能走;
接下来m行 x1 y1 x2 y2 是隧道起点和终点.
一个人要走过所有的隧道,每条隧道只能走一次,求最短时间,起点任选,在隧道内的时间不计。

思路是把隧道抽象成点,边的长度即为起点隧道的出口到终点隧道的入口的距离(BFS),然后就变成了旅行家问题,dp[i][j]表示现在站在i,接下来要走j表示的集合里的点共花费的时间。用j表示集合的方法大概像用5D(0101B)表示接下来要走点1和3。

转移方程:

if(j&(1 << k))dp[i][j] = min(dp[i][j],dp[k+1][j^(1 << k)]+d[i][k+1];
//d[i][k+1]表示i到k+1的距离

AC代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;#define maxn 200007
const int INF=0x3f3f3f3f;
int mx[]={0,0,1,-1};
int my[]={-1,1,0,0};
int n,m;
char a[17][17];
struct node
{int x,y;
}f,ne;
typedef struct tunnel
{node st,en;
}tu;
tu t[17];
int d[17][17];
int dis[17][17];
int dp[17][(1<<15)+7];
int vis[17][(1<<15)+7];
void dfs(int i,int j)
{vis[i][j]=1;if(j==0){dp[i][j]=0;return ;}dp[i][j]=INF;for(int k=0;k<m;k++){if(j&(1<<k)){if(!vis[k+1][j^(1<<k)]) dfs(k+1,j^(1<<k));dp[i][j]=min(dp[i][j],dp[k+1][j^(1<<k)]+d[i][k+1]);}}
}
int main()
{int i,j,k,x,y;while(scanf("%d%d",&n,&m)!=EOF){memset(d,0,sizeof(d));for(i=1;i<=n;i++){scanf("%s",a[i]+1);}for(i=1;i<=m;i++){scanf("%d%d%d%d",&t[i].st.x,&t[i].st.y,&t[i].en.x,&t[i].en.y);}queue <node> q;for(i=1;i<=m;i++){while(!q.empty()) q.pop();memset(dis,-1,sizeof(dis));f=t[i].en;dis[f.x][f.y]=0;q.push(f);while(!q.empty()){f=q.front();q.pop();for(j=0;j<4;j++){ne.x=f.x+mx[j];ne.y=f.y+my[j];if(ne.x>=1&&ne.x<=n&&ne.y>=1&&ne.y<=n&&a[ne.x][ne.y]!='#'&&dis[ne.x][ne.y]==-1){dis[ne.x][ne.y]=dis[f.x][f.y]+1;q.push(ne);}}}for(j=1;j<=m;j++){if(dis[t[j].st.x][t[j].st.y]==-1) d[i][j]=INF;else d[i][j]=dis[t[j].st.x][t[j].st.y];}}memset(vis,0,sizeof(vis));dfs(0,(1<<m)-1);if(dp[0][(1<<m)-1]<INF){printf("%d\n",dp[0][(1<<m)-1]);}else{printf("-1\n");}}return 0;
}

HDU - 4856 Tunnels (预处理+状压dp)相关推荐

  1. Hdu 4856 Tunnels(状压dp)

    题目链接 Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. HDU - 4856 Tunnels(哈密顿路径+状压dp)

    题目链接:点击查看 题目大意:给出一个n*n的正方形网格,其中"."表示可以走的路,"#"表示障碍物,每次可以向上下左右任意方向走1格,花费1单位时间,再给出m ...

  3. Tunnels HDU - 4856 (bfs状压dp)

    Tunnels HDU - 4856 Bob is travelling in Xi'an. He finds many secret tunnels beneath the city. In his ...

  4. HDU 3001 三进制状压DP

    HDU 3001 三进制状压DP N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方 ...

  5. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  6. loj 1316(spfa预处理+状压dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...

  7. hdu 4049 Tourism Planning [ 状压dp ]

    传送门 Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

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

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

  9. HDU 4899 Hero meet devil (状压DP, DP预处理)

    题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...

最新文章

  1. PyTorch、TensorFlow最新版本对比,2021年了你选谁?
  2. 第一次胜过MobileNet的二值神经网络,-1与+1的三年艰苦跋涉
  3. 平均月薪 21.8k、中国开发者占比最高,揭晓 Go 语言真实现状!
  4. Python中将数据矢量化运算所带来的时间加快
  5. 第十五届全国大学生智能车竞赛各分赛区赛道数量以及比赛系统数量
  6. TextView显示颜色高亮的问题
  7. Dynamics 365 Online-Unified User Interface
  8. 男生种草潮流社区edge(嘿市),会是男生的小红书吗?
  9. 使用Python以UCI心脏病数据集为例,进行数据简单分析
  10. 考研复试专业课面试——C++
  11. CSDN的markdown编辑器的语法:字体的样式、大小、颜色怎么调节?建议收藏,让你的博客更漂亮
  12. 单片机音频节奏灯_如何用单片机做出用音乐节奏来控制LED灯?
  13. PDA 无线网络设置
  14. ILI9341的使用之【六】命令二
  15. LaTeX数学公式相关
  16. 利用jQuery插件扩展识别浏览器内核与外壳的类型和版本
  17. 【转】B站最强学习资源汇总(数据科学,机器学习,python)
  18. 搭建Nexus私库使用
  19. AE cs4无法安装的故障
  20. 高中计算机专业满分多少,高考的通用技术和信息技术总分是多少

热门文章

  1. 学习嵌入式系统的误区
  2. 电子计算机系学生会特色活动,计算机系学生会文艺部计划
  3. Qt 将qsqlite数据库中的数据导出为Excel表格
  4. makefile进入子文件夹执行make
  5. 招商银行掌上生活还款服务个人信息处理授权书
  6. JSP —— 关于绝对路径和相对路径
  7. [Python学习第一天]内置数据类型介绍,万物皆对象!
  8. 数据缩放在监督学习中的应用
  9. Linux 安装Nginx教程
  10. 2022年初级会计考试精选模拟题