题目链接:http://poj.org/problem?id=3026
Time Limit: 1000MS Memory Limit: 65536K

Description

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

Problem solving report:

Description: 有x个点,其中有一个是起点,起点有不多于100个人,告诉你一张地图,地图上分布着这些点,起点用'S'表示,其余点用‘A’表示,可以走的路为‘ ’,不能走的为‘#’,求从起点出发到所有点所需的最小路径,可以在任意位置分出任意人向某点前进,保证人够,多人走同一路径距离只算一遍。
Problem solving: 这题题意好难理解……理解后就好办了,题意可以转化为已知x个点的位置,用x-1条边将其连起来,使总边权最小,这就是一道最小生成树问题。因为有‘#’的存在,所以必须要把点与点之间的距离用bfs扫出来,之后写一遍最小生成树,然后就没问题了!注意这一题n和m后面会莫名的出现空格,wa了很多次,需要用gets来处理一下,或者在n和m后面加一个\n。

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 55;
const int MAXM = 55 * 55;
const int inf = 0x3f3f3f3f;
bool vis[MAXN][MAXN], visp[MAXM];
int n, m;
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char mp[MAXN][MAXN];
int hap[MAXM][MAXM], dis[MAXM];
map <pair<int, int>, int> spt;
struct edga {
    int v, w;
    edga() {}
    edga(int v, int w) : v(v), w(w) {}
    bool operator < (const edga &s) const {
        return s.w < w;
    }
};
struct edge {
    int x, y, t;
    edge() {}
    edge(int x, int y, int t) : x(x), y(y), t(t) {}
};
void BFS(int x, int y) {
    queue <edge> Q;
    memset(vis, 0, sizeof(vis));
    vis[x][y] = true;
    Q.push(edge(x, y, 0));
    while (!Q.empty()) {
        edge p = Q.front();
        Q.pop();
        if (spt.count(make_pair(p.x, p.y)))
            hap[spt[make_pair(x, y)]][spt[make_pair(p.x, p.y)]] = p.t;
        for (int i = 0; i < 4; i++) {
            int tx = p.x + dir[i][0];
            int ty = p.y + dir[i][1];
            if (tx >= 0 && tx < n && ty >= 0 && ty < m && mp[tx][ty] != '#' && !vis[tx][ty]) {
                vis[tx][ty] = true; 
                Q.push(edge(tx, ty, p.t + 1));
            }
        }
    }
}
int prim(int s, int n) {
    priority_queue <edga> Q;
    for (int i = 0; i < n; i++) {
        visp[i] = 0;
        dis[i] = inf;
    }
    dis[s] = 0;
    Q.push(edga(s, dis[s]));
    int ans = 0;
    while (!Q.empty()) {
        edga u = Q.top();
        Q.pop();
        if (visp[u.v])
            continue;
        visp[u.v] = 1;
        ans += u.w;
        for (int j = 0; j < n; j++) {
            if (!visp[j] && dis[j] > hap[u.v][j]) {
                dis[j] = hap[u.v][j];
                Q.push(edga(j, dis[j]));
            }
        }
    }
    return ans;
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int cnt = 0;
        spt.clear();
        scanf("%d%d\n", &m, &n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%c", &mp[i][j]);
                if (mp[i][j] == 'A' || mp[i][j] == 'S')
                    spt[make_pair(i, j)] = cnt++;
            }
            getchar();
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (spt.count(make_pair(i, j)))
                    BFS(i, j);
        printf("%d\n", prim(0, cnt));
    }
    return 0;
}