HDU4856:Tunnels (BFS & 状压dp)

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).
InputThe 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 x  1, y  1, x  2, y  2, indicating there is a tunnel with entrence in (x  1, y  1) and exit in (x  2, y  2). It’s guaranteed that (x  1, y  1) and (x  2, y  2) in the map are both empty grid.OutputFor 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*N的图,有M条地下通道,可以任选起点,要求走完所有通道的最短距离,通道有起点和终点S和T,从通道X的T到通道Y的S要在地面上走,在地面不能穿过#格子,每个通道走一次,且通过单个通道距离视为0。

思路:就是旅行商问题,预处理每个通道的终点到其他通道的起点的最短距离,dp[i][j]表示走完i状态的通道最终停在j号通道的最短距离。

# include <bits/stdc++.h>
# define pb push_back
# define mp make_pair
# define inf 0x3f3f3f3f
using namespace std;
char s[20][20];
int n, m, dp[1<<15][20], dis[20][20], d[20][20];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
vector<int>Mp[20][20];
struct node
{
    int a, b, c, d;
}g[20];
bool ok(int x, int y)
{
    if(x<1||y<1||x>n||y>n) return false;
    return true;
}
void bfs(int st)
{
    memset(d, -1, sizeof(d));
    queue<pair<int,int> >q;
    q.push(mp(g[st].c, g[st].d));
    d[g[st].c][g[st].d] = 0;
    while(!q.empty())
    {
        int x=q.front().first, y=q.front().second;
        if(Mp[x][y].size())
            for(int ed:Mp[x][y])
                dis[st][ed] = d[x][y];
        q.pop();
        for(int i=0; i<4; ++i)
        {
            int px = x+dx[i];
            int py = y+dy[i];
            if(ok(px, py) && s[px][py]!='#' && d[px][py]==-1)
            {
                d[px][py] = d[x][y]+1;
                q.push(mp(px,py));
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp, 0x3f, sizeof(dp));
        memset(dis, 0x3f, sizeof(dis));
        for(int i=0; i<=n; ++i) for(int j=0; j<=n; ++j) Mp[i][j].clear();
        for(int i=1; i<=n; ++i) scanf("%s",s[i]+1);
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d%d",&g[i].a,&g[i].b,&g[i].c,&g[i].d);
            Mp[g[i].a][g[i].b].push_back(i);
        }
        for(int i=0; i<m; ++i) bfs(i);
        for(int i=0; i<m; ++i) dp[1<<i][i]=0;
        for(int i=1; i<1<<m; ++i)
            for(int j=0; j<m; ++j)
                if(!(i&(1<<j)))
                    for(int k=0; k<m; ++k)
                        if(i&(1<<k))dp[i|(1<<j)][j] = min(dp[i|(1<<j)][j], dp[i][k]+dis[k][j]);
        int ans = inf;
        for(int i=0; i<m; ++i) ans=min(ans, dp[(1<<m)-1][i]);
        printf("%d\n",ans!=inf?ans:-1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值