[POJ 3026] Borg Maze【BFS+Prim】
Problem:
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
Output
Sample Input
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Sample Output
8 11
Source
Solution:
读了一遍题目都没读懂。。这道题的最小生成树藏得真深。
假设共有 N 个外星人,由于 Borg 团体只能在开始的 'S' 处和有外星人的 'A' 处分裂,我们可以以这 N + 1 个点作为起点跑 BFS 预处理出两两之间的距离,处理出一张具有 N + 1 个点的无向图,跑一遍 Prim 求最小生成树的权值和即为最小代价。
注意读入时先列后行!
Code: O(Tn2)
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> 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; while(!q.empty()){ 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); while(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; }
发表评论