【BZOJ3049】Island Travels,SPFA预处理+状态压缩DP

传送门(权限题)
3049: [Usaco2013 Jan]Island Travels

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 84 Solved: 34
[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’区到达右下角的岛屿。(最优路径不一定只有一条)

写在前面:又到了退役的季节
思路:(测试的时候真的没想状压转移,因为被缩点给卡住了)
一开始看题:woc这是求曼哈顿距离最小生成树吗?
……
看看题应该就想到要求最短路(没错我这么纱布的人也能看出来,所以你们一定也可以= =)
数据范围这么小,要么高维DP要么网络流要么状压DP,而且岛屿只有15个,完全可以做15遍SPFA求出最短路,接下来有两种情况
1.暴力枚举走的顺序,当成全排列,时间 O(15!) (Yveh最初的想法)
2.状态压缩,对岛屿进行编号,用2进制表示第i位是否走过(0 or 1),这样就可以状态转移了,i表示状态,j表示最后走到的岛屿编号
f[i+(1<<k)][j]=min(f[i+(1<<k)][j],f[i][j]+dis[j][k])
其中要求j在i中为1,所以就是i&(1<<j)>0,最终答案就是max(f[(1<<color)-1][i])(1<=i<=color),color为岛屿总数
注意:
1.Yveh没有缩点,跑了所有’X’的SPFA,但时间并没有想象的那么糟糕
2.跑SPFA时我是对各岛屿的代表点重编号,分别为1..color,其他点为color+1,color+2,..n*m.
3.代码略蛋疼,见谅
代码:

#include<bits/stdc++.h>
using namespace std;
int m,n,tot,color,ans=0x7fffffff;
char s[52][52];
bool flag[52][52];
int dis[20][2502],maps[52][52],f[70000][20];
typedef pair<int,int> xy;
xy rep[2502];
queue<xy> q;
void dfs(int x,int y)
{
    if (flag[x][y]) return;
    flag[x][y]=1;
    if (x+1<=n&&s[x+1][y]=='X') dfs(x+1,y);
    if (x-1>0&&s[x-1][y]=='X') dfs(x-1,y);
    if (y+1<=m&&s[x][y+1]=='X') dfs(x,y+1);
    if (y-1>0&&s[x][y-1]=='X') dfs(x,y-1);
}
main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    scanf("%s",s[i]+1);
    for (int i=1;i<=n;i++)
    for (int j=1;j<=m;j++)
    if (s[i][j]=='X'&&!flag[i][j])
    {
        rep[++tot].first=i;
        rep[tot].second=j;//建立重编号到坐标的映射
        maps[i][j]=tot;//建立坐标到编号的映射
        dfs(i,j);
    }
    color=tot;
    for (int i=1;i<=n;i++)
    for (int j=1;j<=m;j++)
    if (!maps[i][j])
    rep[++tot].first=i,
    rep[tot].second=j,
    maps[i][j]=tot;
    /*-------------------------重构图,建立坐标与点编号的映射---------------------*/
    memset(flag,0,sizeof(flag));
    for (int i=1;i<=color;i++)
    {
        memset(dis[i],63,sizeof(dis[i]));
        dis[i][i]=0;
        q.push(rep[i]);
        while (!q.empty())
        {
            int x=q.front().first,y=q.front().second;
            q.pop();
            if (x+1<=n&&s[x+1][y]!='.'&&dis[i][maps[x+1][y]]>dis[i][maps[x][y]]+(s[x+1][y]=='S'))//(x,y)->(x+1,y)
            {
                dis[i][maps[x+1][y]]=dis[i][maps[x][y]]+(s[x+1][y]=='S');
                if (!flag[x+1][y]) q.push(rep[maps[x+1][y]]),flag[x+1][y]=1;
            }
            if (x-1>0&&s[x-1][y]!='.'&&dis[i][maps[x-1][y]]>dis[i][maps[x][y]]+(s[x-1][y]=='S'))//(x,y)->(x-1,y)
            {
                dis[i][maps[x-1][y]]=dis[i][maps[x][y]]+(s[x-1][y]=='S');
                if (!flag[x-1][y]) q.push(rep[maps[x-1][y]]),flag[x-1][y]=1;
            }
            if (y+1<=m&&s[x][y+1]!='.'&&dis[i][maps[x][y+1]]>dis[i][maps[x][y]]+(s[x][y+1]=='S'))//(x,y)->(x,y+1)
            {
                dis[i][maps[x][y+1]]=dis[i][maps[x][y]]+(s[x][y+1]=='S');
                if (!flag[x][y+1]) q.push(rep[maps[x][y+1]]),flag[x][y+1]=1;
            }
            if (y-1>0&&s[x][y-1]!='.'&&dis[i][maps[x][y-1]]>dis[i][maps[x][y]]+(s[x][y-1]=='S'))//(x,y)->(x,y-1)
            {
                dis[i][maps[x][y-1]]=dis[i][maps[x][y]]+(s[x][y-1]=='S');
                if (!flag[x][y-1]) q.push(rep[maps[x][y-1]]),flag[x][y-1]=1;
            }
            flag[x][y]=0;
        }
    }
    /*-------------------------SPFA每个岛屿---------------------*/
    memset(f,63,sizeof(f));
    for (int i=1;i<=color;i++) f[1<<i-1][i]=0;
    for (int i=1;i<=(1<<color)-1;i++)
    for (int j=1;j<=color;j++)
    if (i&(1<<j-1))
    {
        for (int k=1;k<=color;k++)
        if ((i&(1<<k-1))==0) 
        f[i+(1<<k-1)][k]=min(f[i+(1<<k-1)][k],f[i][j]+dis[j][k]);
    }
    /*-------------------------状态压缩DP转移---------------------*/
    for (int i=1;i<=color;i++)
    ans=min(ans,f[(1<<color)-1][i]);
    printf("%d",ans);
}

你可能感兴趣的:(【BZOJ3049】Island Travels,SPFA预处理+状态压缩DP)