Borg Maze
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.
Input
On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space '' stands for an open space, a hash mark
#'' stands for an obstructing wall, the capital letter
A'' stand for an alien, and the capital letter
S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the
S''. At most 100 aliens are present in the maze, and everyone is reachable.
Output
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001
题目类型:最短路
算法分析:题目要求解的实际上是将每个点连起来所需要的最小花费,由于题目说在起点和其他相异个体处会分裂成多个子个体,则最小满足条件的就是每个点都向其它点连边后MST的权值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
/************************************************* Author :supermaker Created Time :2016/3/22 21:07:43 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100 + 66; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; struct Node { int u, v, w; Node (int uu, int vv, int ww) : u (uu), v (vv), w (ww) {} Node () {} bool operator < (const Node &a) const { return w < a.w; } }; char g[66][66]; int row, col, par[maxn], cnt, gg[66][66], len; Node edge[maxn*maxn]; bool vis[66][66], vvis[maxn][maxn]; inline bool InLine (int x, int y) { if (x >= 0 && x <= row - 1 && y >= 0 && y <= col - 1) return true; return false; } void bfs (int x, int y) { memset (vis, false, sizeof (vis)); vis[x][y] = true; queue <Node> qu; qu.push (Node (x, y, 0)); while (!qu.empty ()) { Node tt = qu.front (); qu.pop (); if ((g[tt.u][tt.v] == 'A' || g[tt.u][tt.v] == 'S') && tt.w) if (!vvis[gg[x][y]][gg[tt.u][tt.v]]) { edge[len++] = Node (gg[x][y], gg[tt.u][tt.v], tt.w); vvis[gg[x][y]][gg[tt.u][tt.v]] = vvis[gg[tt.u][tt.v]][gg[x][y]] = true; } for (int i = 0; i < 4; i++) { int tx = tt.u + dx[i], ty = tt.v + dy[i]; if (InLine (tx, ty) && g[tx][ty] != '#' && !vis[tx][ty]) { vis[tx][ty] = true; qu.push (Node (tx, ty, tt.w + 1)); } } } } int UnFind (int val) { if (par[val] == val) return par[val]; return par[val] = UnFind (par[val]); } int Dij () { int res = 0, num = len; for (int i = 0; i < len && num > 1; i++) { if (UnFind (edge[i].u) != UnFind (edge[i].v)) { par[UnFind(edge[i].u)] = UnFind (edge[i].v); res += edge[i].w; num--; } } return res; } int main() { //CFF; //CPPFF; int t; scanf ("%d", &t); while (t--) { memset (gg, 0, sizeof (gg)); cnt = 1; scanf ("%d%d", &col, &row); gets (g[0]); for (int i = 0; i < row; i++) gets (g[i]); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (g[i][j] == 'A' || g[i][j] == 'S') gg[i][j] = cnt++; memset (vvis, false, sizeof (vvis)); len = 0; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (g[i][j] == 'A' || g[i][j] == 'S') bfs (i, j); for (int i = 0; i < maxn; i++) par[i] = i; sort (edge, edge + len); printf ("%d\n", Dij ()); } return 0; } |