向前走莫回头❤

【bzoj 3049】[Usaco2013 Jan]Island Travels(状压dp)

3049: [Usaco2013 Jan]Island Travels

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 97  Solved: 44
[Submit][Status][Discuss]

Description

Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 <= N <= 15) islands, which are located on an R x C grid (1 <= R, C <= 50). An island is a maximal connected group of squares on the grid that are marked as 'X', where two 'X's are connected if they share a side. (Thus, two 'X's sharing a corner are not necessarily connected.) Bessie, however, is arriving late, so she is coming in with FJ by helicopter. Thus, she can first land on any of the islands she chooses. She wants to visit all the cows at least once, so she will travel between islands until she has visited all N of the islands at least once. FJ's helicopter doesn't have much fuel left, so he doesn't want to use it until the cows decide to go home. Fortunately, some of the squares in the grid are shallow water, which is denoted by 'S'. Bessie can swim through these squares in the four cardinal directions (north, east, south, west) in order to travel between the islands. She can also travel (in the four cardinal directions) between an island and shallow water, and vice versa. Find the minimum distance Bessie will have to swim in order to visit all of the islands. (The distance Bessie will have to swim is the number of distinct times she is on a square marked 'S'.) After looking at a map of the area, Bessie knows this will be possible.

  给你一张r*c的地图,有’S’,’X’,’.’三种地形,所有判定相邻与行走都是四连通的。我们设’X’为陆地,一个’X’连通块为一个岛屿,’S’为浅水,’.’为深水。刚开始你可以降落在任一一块陆地上,在陆地上可以行走,在浅水里可以游泳。并且陆地和浅水之间可以相互通行。但无论如何都不能走到深水。你现在要求通过行走和游泳使得你把所有的岛屿都经过一边。Q:你最少要经过几个浅水区?保证有解。
 

Input

* Line 1: Two space-separated integers: R and C. 
* Lines 2..R+1: Line i+1 contains C characters giving row i of the grid. Deep water squares are marked as '.', island squares are marked as 'X', and shallow water squares are marked as 'S'. 

Output

* Line 1: A single integer representing the minimum distance Bessie has to swim to visit all islands.

Sample Input

5 4
XX.S
.S..
SXSS
S.SX
..SX


INPUT DETAILS: There are three islands with shallow water paths connecting some of them.

Sample Output

3
OUTPUT DETAILS: Bessie can travel from the island in the top left to the one in the middle, swimming 1 unit,
and then travel from the middle island to the one in the bottom right, swimming 2 units, for a total of 3 units.



HINT

样例解释:

 5*4的地图,先走到左上角的岛屿,再向下经过1’S’区到达中间的岛屿,再向右经过2’S’区到达右下角的岛屿。(最优路径不一定只有一条)

Source

[Submit][Status][Discuss]

【题解】【状压dp】

【先通过BFS找出每个连通块并逐一编号,再通过SPFA求出每两个连通块之间的最短距离,最后写一个状压dp找出最优解】

【转移方程:g[i+(1<<j)][j]=min(g[i+(1<<j)][j],g[i][k]+dist[k+1][j+1])】 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int d1[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int mp[60][60],mp1[60][60],n,m,d[12010][2],dis[60][60],h,t,maxn;
int f[1<<15][20],ans=0x7fffffff,dist[20][20];
bool p[60][60];
inline void find(int sx,int sy,int k)
{
    memset(p,true,sizeof(p));
    memset(d,0,sizeof(d));
    h=t=0; d[++t][0]=sx; d[t][1]=sy; p[sx][sy]=false;
    mp1[sx][sy]=k;
    while (h!=t)
      {
        int x,y;
        h=(h%12010)+1;
        x=d[h][0]; y=d[h][1];
        for (int i=0;i<4;++i)
         {
            int xx,yy;
            xx=x+d1[i][0]; yy=y+d1[i][1];
            if (xx>0&&yy>0&&xx<=n&&yy<=m&&p[xx][yy]&&mp[xx][yy]==1)
             {
                t=(t%12010)+1;
                d[t][0]=xx; d[t][1]=yy;
                p[xx][yy]=false;
                mp1[xx][yy]=k;
                }
           }
      } 
    return;
}
inline void search(int sx,int sy,int k,int mm)
{
    memset(d,0,sizeof(d));
    memset(p,true,sizeof(p));
    memset(dis,127/3,sizeof(dis));
    int i,j;
    h=t=0;
    d[++t][0]=sx; d[t][1]=sy; p[sx][sy]=false; 
    dis[sx][sy]=0;  dist[k][k]=0;
    while (h!=t)
     { 
       int x,y;
       h=(h%12010)+1;
       x=d[h][0]; y=d[h][1]; p[x][y]=true;
       for (i=0;i<4;++i)
        {
            int xx,yy;
            xx=x+d1[i][0]; yy=y+d1[i][1];
            if (xx>0&&yy>0&&xx<=n&&yy<=m&&mp[xx][yy]&&dis[xx][yy]>dis[x][y])
             {
                if(mp[xx][yy]==1)  dis[xx][yy]=dis[x][y]; 
                if (mp[xx][yy]==2) dis[xx][yy]=dis[x][y]+1;
                if (p[xx][yy])
                 {
                    t=(t%12010)+1;
                    d[t][0]=xx; d[t][1]=yy;
                    p[xx][yy]=false;
                  }
             }
        }
     }
    int b=1;
    for (i=1;i<=n;++i)
     for (j=1;j<=m;++j)
      if (mp1[i][j]==b)
         {dist[k][mp1[i][j]]=min(dist[k][mp1[i][j]],dis[i][j]);  b++;}
        else
         if (b>mm) return;
}
int main()
{
    int i,j,l;
    int x=0,y=0;
    memset(dist,127/3,sizeof(dist));
    scanf("%d%d\n",&n,&m);
    for (i=1;i<=n;++i)
     {
        for (j=1;j<=m;++j)
         {
          char c;
          c=getchar();
          if (c=='X') {mp[i][j]=1; if (!x&&!y) {x=i; y=j;}}
          if (c=='.') mp[i][j]=0;
          if (c=='S') mp[i][j]=2;
        }
        getchar();
     }
    int k=0;
    for (i=1;i<=n;++i)
     for (j=1;j<=m;++j)
      if (mp[i][j]==1&&!mp1[i][j])
        find(i,j,++k);
    int tt=1;
    for (i=1;i<=n;++i)
     for (j=1;j<=m;++j)
       if (tt>k) break;
        else
         if (mp1[i][j]==tt)
           {search(i,j,tt,k); tt++;}
    memset(f,127/3,sizeof(f));
    for (i=0;i<k;++i) f[1<<i][i]=0;
    for (i=0;i<(1<<k);++i)
     for (j=0;j<k;++j)
      if ((i>>j)&1)
       for (l=0;l<k;++l)
        f[i|(1<<l)][l]=min(f[i|(1<<l)][l],f[i][j]+dist[j+1][l+1]);
    for (i=0;i<k;++i)
     ans=min(ans,f[(1<<k)-1][i]);
    printf("%d\n",ans);
    return 0;
}


posted @ 2016-11-16 17:32  lris0-0  阅读(93)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m