Time limit:1000ms Memory limit:65536k
Description
The Borg is a 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 are linked to the collective by a sophisticated subspace network that insures each member are given con Stant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg-estimate the minimal cost O F 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, the beginning of the search is conducted by a large group of over individuals. Whenever an alien was assimilated, or at the beginning of the search, the group could split in and more groups (but their Consciousness is still collective.). The cost of searching a maze are definied as the total distance covered by all the groups involved in the search together. That's, if the original group walks five steps, then splits into both groups each walking three steps, the total distance Is 11=5+3+3.
Input
On the first line of input there are one integer, N <=, giving the number of test cases in the input. Each test case starts with a line containg the 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 A ' stand for an alien, and the capital LetterS "stands for the start of the search. The perimeter of the maze is always closed, i.e., and there is the no-to-get out from the coordinate of the "S". At most of aliens is 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
Give you one by one graphs to solve the smallest spanning tree between the characters ' A ' and ' S ' in the graph
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath&
Gt
#include <queue> #include <algorithm> using namespace std;
const int INF = 0X3F3F3F3F;
int dir[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
Char s[55][55];
int n,m;
int vis[55][55];
struct node{int v,w,next;
}E[100100];
int head[110],top; void creat (int u,int v,int w) {e[top].v = v;
E[TOP].W = W;
E[top].next = Head[u];
Head[u] = top++;
} struct node{int x,y,step;
Node () {} node (int _x,int _y,int _step): X (_x), Y (_y), step (_step) {}};
BOOL VIS[50][50]; BOOL Judge (int x,int y) {if (x>=0 && x<n&&y>=0 &&y<n && s[x][y]!= ' # ' && AMP;!
Vis[x][y]) return true;
return false;
} void BFs (int x,int y) {memset (vis,false,sizeof (Vis));
queue<node>q;
Q.push (Node (x,y,0));
Vis[x][y] = true; while (! Q.empty ()) {Node u = q.front ();
Q.pop (); if (Vis[u.x][u.y]) Creat (Vis[x][y],vis[u.x][u.y],u.step);
Node v;
for (int i = 0;i<4;i++) {v.x = u.x+dir[i][0];
V.Y = U.y + dir[i][1];
if (Judge (V.X,V.Y)) {v.step = u.step+1; VIS[V.X][V.Y] = true;
Q.push (v);
}}}} int dis[110];
BOOL vist[110];
int main () {int T;
scanf ("%d", &t);
while (t--) {scanf ("%d%d\n", &m,&n);
int num = 0;
memset (vis,0,sizeof (VIS));
memset (head,-1,sizeof (Head));
top = 0;
for (int i = 0;i<n;i++) {gets (s[i]);
for (int j = 0;j<m;j++) {if (s[i][j] = = ' A ' | | s[i][j] = = ' s ') {vis[i][j] = ++num;
}}} for (int i = 0;i<n;i++) {for (int j = 0;j<m;j++) {
if (s[i][j] = = ' A ' | | s[i][j] = = ' s ') {BFS (i,j); }}} memset (Dis,inf,sizeof (DIS));
for (int i = Head[1];i!=-1;i = E[i].next) dis[e[i].v] = E[I].W;
memset (vist,false,sizeof (vist));
Vist[1] = true;
int ans = 0;
for (int i = 1;i<num;i++) {int Mi = Inf,flag;
for (int j = 1;j<=num;j++) {if (Dis[j]<mi &&!vist[j]) {Mi = Dis[j];
flag = j; }} Vist[flag] = true;
Ans + = Mi;
for (int j = Head[flag];j!=-1;j = E[j].next) {dis[e[j].v] = min (DIS[E[J].V],E[J].W);
}} printf ("%d\n", ans);
} return 0;
}