poj 3026 Borg Maze bfs+最小產生樹,pojborg
Language:DefaultBorg Maze
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 8829 |
|
Accepted: 2954 |
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
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
Source Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001 |
題意:在N*M的迷宮上,從S點出發找一條最短的路徑走遍所有的A,且在S和A處可以分成多組同時走,可以分叉,這樣就先求出所有點兩兩之間的距離,找到一棵最小產生樹,樹上所有邊的和就是要求的最小值。
代碼:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define Maxn 1005using namespace std;struct Node{ int x,y,step; Node(){} Node(int a,int b,int c) { x=a,y=b,step=c; }};struct Edge{ int a,b; int len;};Edge edge[100000];Node node[100000];int cmp[Maxn][Maxn];int father[Maxn];int N,M,edgenum;int visit[Maxn][Maxn];int dir[4][2]={0,-1,0,1,1,0,-1,0};int ccmp(Edge x,Edge y){ return x.len<y.len;}bool ISok(int x,int y){ if (x>=0&&x<N&&y>=0&&y<M&&!visit[x][y]&&cmp[x][y]!=-1) return true; return false;}int find_father(int x){ if (x!=father[x]) father[x]=find_father(father[x]); return father[x];}void bfs(int x,int y){ Node st; queue<Node>Q; while (!Q.empty()) Q.pop(); memset(visit,0,sizeof(visit)); st.x=x; st.y=y; st.step=0; Q.push(st); visit[x][y]=1; while (!Q.empty()) { st=Q.front(); Q.pop(); for (int i=0;i<4;i++) { int dx=st.x+dir[i][0]; int dy=st.y+dir[i][1]; if (ISok(dx,dy)) { visit[dx][dy]=1; Q.push(Node(dx,dy,st.step+1)); if (cmp[dx][dy]>=1) { edge[edgenum].a=cmp[x][y]; edge[edgenum].b=cmp[dx][dy]; edge[edgenum].len=st.step+1; edgenum++; } } } }}void Kruskal(int n){ int r=1,ans=0; for (int i=0;i<n;i++) { int aa=find_father(edge[i].a); int bb=find_father(edge[i].b); if (aa!=bb) { r++; ans+=edge[i].len; father[aa]=bb; if (r==n) break; } } printf("%d\n",ans);}int main(){ int cas; scanf("%d",&cas); while (cas--) { edgenum=0; scanf("%d%d",&M,&N); int num=1; char str[1000]; gets(str); //小心是一組空格,必須用gets for (int i=0;i<N;i++) { for (int j=0;j<M;j++) { char ch; scanf("%c",&ch); if (ch==' ') cmp[i][j]=0; else if(ch=='#') cmp[i][j]=-1; else { cmp[i][j]=num; num++; } } getchar(); } for (int i=0;i<N;i++) { for (int j=0;j<M;j++) { if (cmp[i][j]>0) bfs(i,j); } } sort(edge,edge+edgenum,ccmp); for (int i=0;i<num;i++) father[i]=i; Kruskal(edgenum); } return 0;}