HDU - 4856 Tunnels (预处理+状压dp)
题目
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;
}