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.
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,然后S再去找其他的A,求所需的最短的路径。
分析:
看完题目之后就可以发现,这其实就是用最短的路线将所有的S和A连起来,即最小生成树。可是在找最小生成树之前,构图是必须的,所以我们要先找出地图中的S或A点到其他所有A或S点的距离,这就是搜索的范畴了,可以用BFS来求出。
注意点:要先用gets()吃掉输入m和n后的回车符,然后再输入地图。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define maxn 1010
#define maxm 100010
#define inf 9999999
struct edge{
int x,y,d,next;
}g[maxm];
struct data{
int x,y;
}p[110];
queue<data> q;
int t,n,m;
char s[110][110];
int tot,first[maxn],pre[maxn],ans,sum,v[110][110],tx,ty;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void add(int x,int y,int d){
tot++;
g[tot].x=x;
g[tot].y=y;
g[tot].d=d;
g[tot].next=first[x];
first[x]=tot;
tot++;
g[tot].x=y;
g[tot].y=x;
g[tot].d=d;
g[tot].next=first[y];
first[y]=tot;
}
void prim(){
int t,i,j,k,min,lc[maxn];
for(i=1;i<=n;i++)
lc[i]=inf;
memset(pre,0,sizeof(pre));
for(t=first[1];t!=-1;t=g[t].next)
lc[g[t].y]=g[t].d;
for(i=2;i<=n;i++){
pre[i]=1;
}
lc[1]=0;
for(i=2;i<=n;i++){
min=inf;
for(j=2;j<=n;j++)
if(lc[j]<min&&lc[j]!=0){
min=lc[j];
k=j;
}
ans+=min;
lc[k]=0;
for(t=first[k];t!=-1;t=g[t].next)
if(g[t].d<lc[g[t].y]){
lc[g[t].y]=g[t].d;
pre[g[t].y]=k;
}
}
}
void bfs(int i){
while(!q.empty())
q.pop();
data tmp=p[i];
q.push(tmp);
v[tmp.x][tmp.y]=0;
while(!q.empty()){
tmp=q.front();
q.pop();
for(int k=0;k<4;k++){
tx=tmp.x+dx[k];
ty=tmp.y+dy[k];
if(tx>=1&&tx<=n&&ty>=0&&ty<=m&&v[tx][ty]==-1&&s[tx][ty]!='#'){
v[tx][ty]=v[tmp.x][tmp.y]+1;
data t;
t.x=tx;
t.y=ty;
q.push(t);
}
}
}
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&m,&n);
gets(s[0]);
for(int i=1;i<=n;i++){
gets(s[i]);
}
sum=0;
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
if(s[i][j]=='A'||s[i][j]=='S'){
sum++;
p[sum].x=i;
p[sum].y=j;
}
}
}
tot=0;
memset(first,-1,sizeof(first));
for(int i=1;i<sum;i++){
memset(v,-1,sizeof(v));
bfs(i);
for(int j=i+1;j<=sum;j++){
add(i,j,v[p[j].x][p[j].y]);
}
}
ans=0;
n=sum;
prim();
printf("%d\n",ans);
}
return 0;
}