POJ 3026 Borg Maze 最小生成树Prim+BFS求最短路

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16866   Accepted: 5450

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

题意:从S点出发去捉所有的A,从S出发时可以有多个捉A的,求所有捉A人的最短路的和

题解:这个题要求所有路的和最短,可以转化为求从S到每个节点A的最小生成树,这个题如果要用Kruskal,需要用bfs求每两个点之间的最短路,必定会超时,所以选择Prim,再每次更新相连节点的最短路

#include
#include
#include
#include
#include
#include
#define MAXN 10100
#define INF 0x7fffffff
using namespace std;
struct s
{
	int x;
	int y;
	int step;
	int id;
}p[MAXN],a,b;
char map[55][55];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int pri[MAXN][MAXN];
int v[55][55];
int v1[MAXN];
int dis[MAXN];
int n,m,k;
int sum;
int check(int xx,int yy)
{
	if(xx<0||xx>=m||yy<0||yy>=n||map[xx][yy]=='#') return 0;

	if(v[xx][yy]) return 0;

	return 1;
}
void bfs(int aa,int bb,int num)
{
	int i;
	memset(v,0,sizeof(v));
	queueq;
	a.x=aa;
	a.y=bb;
	a.step=0;
	v[aa][bb]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		for(i=1;i

 

 

 

 

你可能感兴趣的:(BFS,最短路,最小生成树)