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<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
#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));
	queue<s>q;
	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<k;i++)        //如果此点和记录的点相同,记录路径
		{
			if(p[i].x==a.x&&p[i].y==a.y)
			{
				pri[num][p[i].id]=pri[p[i].id][num]=a.step;
			}
		}
		for(i=0;i<4;i++)
		{
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
			if(check(b.x,b.y))
			{
				b.step=a.step+1;
				v[b.x][b.y]=1;
				q.push(b);
			}
		}
	}
}
void prime()
{
	int i,e,j;
	int M;
	memset(v1,0,sizeof(v1));
	for(i=1;i<k;i++)
	dis[i]=pri[1][i];
	v1[1]=1;
	sum=0;
	for(i=1;i<k;i++)
	{
		M=INF;
		for(j=1;j<k;j++)
		{
			if(v1[j]==0&&dis[j]<M)
			{
				M=dis[j];
				e=j;
			}
		}
		if(M==INF)
		break;
		v1[e]=1;
		sum+=M;
		for(j=1;j<k;j++)
		{
			if(v1[j]==0)
			dis[j]=min(dis[j],pri[e][j]);
		}
	}
	printf("%d\n",sum);
}
int main()
{
	int t,i,j;
	char ch[200];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		gets(ch);
		k=1;
        for(i=0;i<m;i++)
        {
            gets(map[i]);
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
               if(map[i][j]=='A'||map[i][j]=='S')
               {
                   p[k].x=i;
                   p[k].y=j;
                   p[k].id=k;
                   k++;
                }
            }
        }
        for(i=1;i<k;i++)
        {
        	int bx=p[i].x;
        	int by=p[i].y;
        	bfs(bx,by,i);
		}
	    prime();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/80699367