BFS寻找每个点到其他点的最小距离

再状压DP,DP[i][j] i二进制表示每个点的到达状态,j为当前所在点

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define pii pair<int,int>
#define CLR(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f;
const int maxn = 40000;
char maze[20][20];
int vis[20][20];
int dis[20][20][20][20];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int n,m; int Dis[20][20][20][20]; struct Node{
int x,y;
int d;
Node(int x,int y,int d){
this->x = x;
this->y = y;
this->d = d;
}
};
queue<Node> Queue; void bfs(int x,int y)
{
vis[x][y] = 1;
dis[x][y][x][y] = 0;
Queue.push(Node(x,y,0));
while(Queue.size()){
int nx = Queue.front().x;
int ny = Queue.front().y;
int nd = Queue.front().d;
Queue.pop();
for(int i=0;i<4;i++){
if(nx + dx[i] >=1 && nx + dx[i] <=n && ny + dy[i] >=1 && ny + dy[i] <= n){
if(!vis[nx + dx[i]][ny + dy[i]] && maze[nx + dx[i]][ny + dy[i]] == '.'){
dis[x][y][nx + dx[i]][ny + dy[i]] = nd + 1;
vis[nx + dx[i]][ny + dy[i]] = 1;
Queue.push(Node(nx + dx[i],ny + dy[i],nd + 1));
}
}
}
}
} int dp[maxn][20];
int POW[20];
int mm[20][5];
int flag = 0; void solve()
{
POW[0] = 1;
for(int i=1;i<20;i++){
POW[i] = POW[i-1]*2;
} CLR(dp,0x3f);
for(int i=0;i<m;i++)
dp[1<<i][i] = 0; for(int i=1;i<POW[m];i++){
for(int j=0;j<m;j++){
if((i&POW[j]) == POW[j]){ int n = i^POW[j];
for(int k=0;k<m;k++){
if( (n&POW[k]) == POW[k]){
int a = mm[k][3];
int b = mm[k][4];
int aa = mm[j][1];
int bb = mm[j][2]; dp[i][j] = min(dp[n][k] + dis[a][b][aa][bb],dp[i][j]);
// cout<<dp[i][j]<<endl;
}
}
}
}
} } int main()
{
// freopen("in.txt","r",stdin);
while(cin>>n>>m){
for(int i=1;i<=n;i++){
scanf("%s",maze[i]+1);
}
for(int i=0;i<20;i++) for(int j=0;j<20;j++) for(int k=0;k<20;k++) for(int l=0;l<20;l++) dis[i][j][k][l] = INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(maze[i][j] == '.'){
CLR(vis,0);
bfs(i,j);
}
}
}
for(int i=0;i<m;i++){
cin>>mm[i][1]>>mm[i][2]>>mm[i][3]>>mm[i][4];
}
solve();
int ans = INF;
for(int i=0;i<m;i++){
ans = min(dp[POW[m]-1][i],ans);
}
if(ans == INF){
cout<<-1<<endl;
}
else
cout<<ans<<endl;
}
return 0;
}

Tunnels HDU - 4856的更多相关文章

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

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

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

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

  3. hdu 4856 Tunnels

    http://acm.hdu.edu.cn/showproblem.php?pid=4856 这道题就是搜索BFS+状压dp,把所经过的隧道的状态用二进制表示,然后dp就行.bfs求出每两个隧道的最短 ...

  4. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

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

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

  6. hdu 4856 Tunnels 状态压缩dp

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

  7. HDU 4856 (状态压缩DP+TSP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4856 题目大意:有一个迷宫.迷宫里有些隧道,每个隧道有起点和终点,在隧道里不耗时.出隧道就耗时,你的 ...

  8. HDU 4856

    http://acm.hdu.edu.cn/showproblem.php?pid=4856 西安邀请赛的一道题,这道题我们当时在现场最后1h才发现时状态压缩dp,惊险写出 现在回头想发现当时有点呆, ...

  9. 状压dpHDU - 4856

    J - Tunnels HDU - 4856 题目大意:地图上有些管道,在管道行走里不需要花费时间,但从一个管道的出口走到另一个管道的入口则需要花费时间,问走完所有管道最短的时间,如果不行,则输出-1 ...

随机推荐

  1. linux服务器挂掉自动重启脚本(转)

    实现原理主要是使用linux提供的crontab机制,定时查询服务器进程是否存在,如果宕机则执行我们预设的重启脚本. 首先我们要向crontab加入一个新任务 sudo crontab -e #进入编 ...

  2. IoC之AutoFac(二)——解析服务

    阅读目录 一 Resolve方法 二 TryResolve和ResolveOptional方法 三 解析服务时传参 3.1 可用参数类型 3.2 带反射组件的参数 3.3 具有Lambda表达式组件的 ...

  3. Nodejs 使用 es module (import/export)

  4. 稀疏贴图 SparseTexture

    官方文档:https://docs.unity3d.com/Manual/SparseTextures.html 是一种虚拟贴图,可以动态的加载或卸载某一块tile.从而实现超大尺寸贴图的加载. 更新 ...

  5. django项目添加utf-8编码支持中文

    代码中出现中文会报错: Non-ASCII character '...' in file ......models.py on line ......., but no encoding decla ...

  6. atom汉化

    Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器. 推荐一下 Atom官方网站https://atom.io/ GitHub 以后肯定会通过官方模块把 Atom 和 GitHub 进 ...

  7. Spring-boot之 rabbitmq

    今天学习了下spring-boot接入rabbitmq. windows下的安装:https://www.cnblogs.com/ericli-ericli/p/5902270.html 使用博客:h ...

  8. iframe子页面与父页面元素的访问以及js变量的访问[zhuan]

    https://www.cnblogs.com/Capricorn-HCL/articles/4216302.html

  9. BrainFuck 指令

    BrainFuck只有八条指令: 指令 含义 等价的C代码 > 指针加一 ++ptr; < 指针减一 --ptr; + 指针指向的字节的值加一 ++*ptr; - 指针指向的字节的值减一 ...

  10. ant 通配符

    ant 通配符 我们常用的匹配模式有ANT模式,比如acegi可以用PATTERN_TYPE_APACHE_ANT来使用ANT匹配模式,那什么是ANT匹配模式呢.   ANT通配符有三种:     通 ...