[POJ 3026] Borg Maze【BFS+Prim】

  • 2018-01-04
  • 0
  • 0


Time Limit: 1000MS Memory Limit: 65536K


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.


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.


For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input

6 5
# # A#
#S  ##
7 7
#    A#
# S ###
#     #

Sample Output



Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001



假设共有 N 个外星人,由于 Borg 团体只能在开始的 'S' 处和有外星人的 'A' 处分裂,我们可以以这 N + 1 个点作为起点跑 BFS 预处理出两两之间的距离,处理出一张具有 N + 1 个点的无向图,跑一遍 Prim 求最小生成树的权值和即为最小代价。


Code: O(Tn2)

using namespace std;
const int dx[4] = { 0, 1, 0,-1};
const int dy[4] = { 1, 0,-1, 0};

int T, R, C;
char mp[55][55];
int id[55][55], N, dis[105][105];

struct Point{
	int x, y, step;
	Point() {}
	Point(int x, int y, int step): x(x), y(y), step(step) {}
	inline bool isLegal() {return x > 0 && x <= R && y > 0 && y <= C && mp[x][y] != '#';}

struct Queue{
	Point node[2505];
	int fr, re;
	inline void clear() {fr = re = 0;}
	inline bool empty() {return fr == re;}
	inline void push(const Point &x) {node[re++] = x;}
	inline Point front_pop() {return node[fr++];}
} q;

bool vis[55][55];

inline void BFS(const int &startid, const int &startx, const int &starty){
	dis[startid][startid] = 0;
	memset(vis, 0, sizeof(vis)), q.clear();
	q.push(Point(startx, starty, 0)), vis[startx][starty] = 1;
		Point u = q.front_pop();
		for(register int i = 0; i < 4; i++){
			Point v(u.x + dx[i], u.y + dy[i], u.step + 1);
			if(!v.isLegal() || vis[v.x][v.y]) continue;
			if(mp[v.x][v.y] == 'S' || mp[v.x][v.y] == 'A') dis[startid][id[v.x][v.y]] = v.step;
			q.push(v), vis[v.x][v.y] = 1;

bool inMST[105];
int curdis[105];

inline int Prim(const int &startid){
	int minWeight = 0;
	memset(inMST, 0, sizeof(inMST));
	memset(curdis, 0x3f, sizeof(curdis)), curdis[startid] = 0;
	for(register int i = 0; i <= N; i++){
		int minId = 101;
		for(register int j = 0; j <= N; j++)
			if(!inMST[j] && curdis[j] < curdis[minId]) minId = j;
		inMST[minId] = 1, minWeight += curdis[minId];
		for(register int j = 0; j <= N; j++)
			curdis[j] = min(curdis[j], dis[minId][j]);
	return minWeight;

int main(){
	scanf("%d", &T);
		scanf("%d%d", &C, &R);
		for(register int i = 0; i <= R; i++) gets(mp[i] + 1);  // Read for an extra time to skip the empty line
		N = 0;
		for(register int i = 1; i <= R; i++)
			for(register int j = 1; j <= C; j++)
				if(mp[i][j] == 'S') id[i][j] = 0;
				else if(mp[i][j] == 'A') id[i][j] = ++N;
		for(register int i = 1; i <= R; i++)
			for(register int j = 1; j <= C; j++)
				if(mp[i][j] == 'S' || mp[i][j] == 'A') BFS(id[i][j], i, j);
		printf("%d\n", Prim(0));
	return 0;





OPEN AT 2017.12.10

If the code cannot be displayed normally, please refresh the page.


- Theme by Qzhai