洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels
题目描述
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:你最少要经过几个浅水区?保证有解。
输入输出格式
输入格式:-
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'.
- Line 1: A single integer representing the minimum distance Bessie has to swim to visit all islands.
输入输出样例
5 4 XX.S .S.. SXSS S.SX ..SX
3
说明
There are three islands with shallow water paths connecting some of them.
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.
【题解】
很撩人的一道题。第一次见隐式图SPFA,不过还算好理解,看看代码可以理解差不多。
很裸的状压DP。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 const int INF = 0x3f3f3f3f; 3 const int MAXN = 15 + 1; 4 const int MAXR = 50 + 5; 5 const int MAXC = 50 + 5; 6 const int dx[4] = {0,0,1,-1}; 7 const int dy[4] = {1,-1,0,0}; 8 9 inline void read(int &x) 10 { 11 x = 0;char ch = getchar();char c = ch; 12 while(ch > '9' || ch < '0')c = ch, ch = getchar(); 13 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 14 if(c == '-')x = -x; 15 } 16 inline int min(int a, int b){return a > b ? b : a;} 17 18 char g[MAXR][MAXC];int flag[MAXR][MAXC],b[MAXR][MAXC],num[MAXN]; 19 int r,c; 20 int d[MAXR][MAXC]; 21 22 struct Node 23 { 24 int x,y; 25 }block[MAXN][MAXN]; 26 27 std::queue <Node> q;int cnt; 28 inline void bfs(int sx, int sy) 29 { 30 q.push(Node{sx,sy}); 31 b[sx][sy] = 1; 32 flag[sx][sy] = cnt; 33 ++ num[cnt]; 34 block[cnt][num[cnt]] = Node{sx, sy}; 35 while(!q.empty()) 36 { 37 Node now = q.front(); 38 q.pop(); 39 for(int i = 0;i < 4;++ i) 40 { 41 int xx = now.x + dx[i]; 42 int yy = now.y + dy[i]; 43 if(xx <= r && xx > 0 && yy <= c && yy > 0 && !b[xx][yy] && g[xx][yy] == 'X') 44 { 45 q.push(Node{xx, yy}); 46 b[xx][yy] = 1; 47 flag[xx][yy] = cnt; 48 ++ num[cnt]; 49 block[cnt][num[cnt]] = Node{xx, yy}; 50 } 51 } 52 } 53 } 54 55 int dis[MAXR][MAXC]; 56 void SPFA(int n) 57 { 58 memset(b, 0, sizeof(b)); 59 memset(dis, 0x3f, sizeof(dis)); 60 for(int i = 1;i <= num[n];++ i) 61 { 62 b[block[n][i].x][block[n][i].y] = 1; 63 dis[block[n][i].x][block[n][i].y] = 0; 64 q.push(block[n][i]); 65 } 66 Node now; 67 while(!q.empty()) 68 { 69 now = q.front(); 70 q.pop(); 71 b[now.x][now.y] = 0; 72 for(int i = 0;i < 4;++ i) 73 { 74 int xx = now.x + dx[i]; 75 int yy = now.y + dy[i]; 76 if(xx <= 0 || xx > r || yy <= 0 || yy > c || g[xx][yy] == '.')continue; 77 if(g[xx][yy] == 'X') 78 { 79 if(dis[xx][yy] > dis[now.x][now.y]) 80 { 81 dis[xx][yy] = dis[now.x][now.y]; 82 if(!b[xx][yy]) 83 { 84 b[xx][yy] = true; 85 q.push(Node{xx, yy}); 86 } 87 } 88 d[flag[xx][yy]][n] = d[n][flag[xx][yy]] = min(d[n][flag[xx][yy]], min(d[flag[xx][yy]][n],dis[xx][yy])); 89 } 90 else if(g[xx][yy] == 'S') 91 { 92 if(dis[xx][yy] > dis[now.x][now.y] + 1) 93 { 94 dis[xx][yy] = dis[now.x][now.y] + 1; 95 if(!b[xx][yy]) 96 { 97 b[xx][yy] = true; 98 q.push(Node{xx, yy}); 99 } 100 } 101 } 102 } 103 } 104 } 105 106 inline void init() 107 { 108 read(r);read(c); 109 for(int i = 1;i <= r;++ i) 110 scanf("%s", g[i] + 1); 111 for(int i = 1;i <= r;++ i) 112 for(int j = 1;j <= c;++ j) 113 if(!b[i][j] && g[i][j] == 'X') 114 { 115 ++ cnt; 116 bfs(i, j); 117 } 118 memset(d, 0x3f, sizeof(d)); 119 for(int i = 1;i <= cnt;++ i) 120 d[i][i] = 0, SPFA(i); 121 } 122 123 //状压方程:F[S][j]表示走过点集S到达j的最短路径 124 //转移:F[S/j][k] + dis[k,j] 125 int dp[(1 << MAXN)][MAXN],ans; 126 127 inline void DP() 128 { 129 memset(dp, 0x3f, sizeof(dp)); 130 int tmp = (1 << cnt); 131 for(int i = 1;i <= cnt;++ i) 132 dp[1 << (i - 1)][i] = 0; 133 for(int S = 1;S < tmp;++ S) 134 { 135 for(int i = 1;i <= cnt;++ i) 136 { 137 if(!(S & (1 << (i - 1))))continue; 138 for(int j = 1;j <= cnt;++ j) 139 { 140 if(j == i || !(S & (1 << (j - 1))))continue; 141 dp[S][i] = min(dp[S][i], dp[S ^ (1 << (i - 1))][j] + d[j][i]); 142 } 143 } 144 } 145 } 146 147 inline void put() 148 { 149 int S = (1 << cnt) - 1; 150 ans = INF; 151 for(int j = 1;j <= cnt;++ j) 152 ans = min(ans, dp[S][j]); 153 printf("%d", ans); 154 } 155 156 int main() 157 { 158 init(); 159 DP(); 160 put(); 161 return 0; 162 }