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条通道的起点距离;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=+,M=1e6+,inf=1e9+,MOD=1e9+;
const LL INF=1e18+,mod=1e9+; int n,m,vis[N][N];
char a[N][N];
struct is
{
int s,t,e,d;
} q[N];
int check(int x,int y)
{
if(x<=||x>n||y<=||y>n)return ;
return ;
}
int dis[N][N][N][N];
int xx[]= {,,-,};
int yy[]= {,,,-};
void bfs(int s,int t)
{
queue<pair<int,int> >q;
q.push(make_pair(s,t));
memset(vis,,sizeof(vis));
vis[s][t]=;dis[s][t][s][t]=;
while(!q.empty())
{
pair<int,int> p=q.front();
q.pop();
for(int i=; i<; i++)
{
int x=p.first+xx[i];
int y=p.second+yy[i];
if(check(x,y)&&!vis[x][y]&&a[x][y]=='.')
{
vis[x][y]=;
dis[s][t][x][y]=dis[s][t][p.first][p.second]+;
q.push(make_pair(x,y));
}
}
}
}
int dp[][N];
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(dis,-,sizeof(dis));
for(int i=; i<=n; i++)
scanf("%s",a[i]+);
for(int i=; i<=m; i++)
scanf("%d%d%d%d",&q[i].s,&q[i].t,&q[i].e,&q[i].d);
for(int i=;i<=n;i++)for(int j=;j<=n;j++)bfs(i,j);
memset(dp,-,sizeof(dp));
for(int i=;i<=(<<m)-;i++)
{
for(int j=;j<=m;j++)
{
if((<<(j-))&i)
{
int now=i-(<<(j-));
if(!now)
{
dp[i][j]=;
continue;
}
for(int k=;k<=m;k++)
{
if(now&(<<(k-)))
{
if(dp[now][k]!=-&&dis[q[k].e][q[k].d][q[j].s][q[j].t]!=-)
{
int temp=dp[i][j];
dp[i][j]=dp[now][k]+dis[q[k].e][q[k].d][q[j].s][q[j].t];
if(temp!=-)dp[i][j]=min(dp[i][j],temp);
}
}
}
}
}
}
int ans=inf;
for(int i=;i<=m;i++)
if(dp[(<<m)-][i]!=-)ans=min(ans,dp[(<<m)-][i]);
if(ans!=inf)printf("%d\n",ans);
else printf("-1\n");
}
return ;
}

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. mybatisplus ssm配置要点

    本以为不难,但也捣鼓了大半天,记录要点如下: 在pom中引入mybatis plus相关包 <!-- mybatis-plus框架包 start --> <dependency> ...

  2. Keras RetinaNet github项目安装

    在存储库目录/keras-retinanet/中,执行pip install . --user 后,出现错误: D:\>cd D:\JupyterWorkSpace\keras-retinane ...

  3. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. centOS 7 设置DNS方法 同之前版本不同

    在CentOS 7下,手工设置 /etc/resolv.conf 里的DNS,过了一会,发现被系统重新覆盖或者清除了.和CentOS 6下的设置DNS方法不同,有几种方式: 1.使用全新的命令行工具 ...

  5. python-soap接口请求

    一.环境准备 方法一: >pip3 install suds >pip3 install suds-jurko 因在线安装报错,所以直接下载安装包. 方法二: 1.suds库下载地址:ht ...

  6. 工具方法: jQuery.方法() $.extend (小计)

    $.extend(布尔值,目标对象,合并对象,……)                布尔值 : true,深拷贝(递归拷贝)   false,浅拷贝(非递归拷贝)                   ...

  7. if语句&switch&Scanner

    if流程控制语句: if 语句的用语法如下: if(boolean表达式){ //如果条件为true那么执行 } 只有条件为true时才会执行,否则执行if语句后面的代码. 实例代码: public ...

  8. 排序之冒泡排序(bubblesort)

    package com.pailian; /* * 冒泡排序 * 比较相邻的俩位数,这样每轮比较都会出现一个最大值或最小值 * 下一轮比较就会减少一次(因为已经知道了一个最大值或最小值) * 注意根据 ...

  9. PHP图像处理(GD库)

    一.图像处理概述 1.开启GD2图像扩展库 ①PHP不仅限于只产生HTML的输出,还可以创建与操作多种不同格式的图像文件.PHP提供了一些内置的图像处理函数,也可以使用GD函数库创建新图像或处理已有的 ...

  10. java中String字符串的==解析

    今天不知道怎么看了下string的==的问题,本身我觉得我这个水平去判断几个字符串相等还能出问题?呵呵,真的出了大问题,根本原因在于对java字节码的不了解. 首先,==运算符比较的是两个变量所指向的 ...