Description
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
811
题意:计算从S 到每个点A的最小总距离 <重复的路径不计算>
思路:<来源于 Jun博客>
将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;
则 问题可看做 求 改图的最小生成树;利用Prim求解即可;
代码::/**** 我认为这道题难想也是难理解的就是建距离图那一块 在这个代码中 自己设置点 即mp[][]=dot 在计算距离的时候 是计算点i到dot的距离 */ #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define INF 99999999 char str[300][300]; int mp[300][300],dot,dx[]= {-1,0,0,1},dy[]= {0,-1,1,0},n,m;///cnt用于BFS中的结束队列 int dis[300][300],vist[300][300]; struct node { int x,y,step; } s[500]; int panduan(int a,int b) { if( a<1 || a>n || b<1 || b>m || vist[a][b]==1 || mp[a][b]==-1 ) return 0; return 1; } int bfs()///BFS查找与建立距离图 <注意看建距离图怎么建的,> { node next,temp; int x,y,i,j; queue<node>q; memset(dis,0,sizeof(dis));///计算结构体里面字符之间的距离 for(i=1; i<=dot; i++)///计算每个点到其他点的距离 { while(!q.empty()) q.pop(); memset(vist,0,sizeof(vist));///bfs里面的标记数组 s[i].step = 0; vist[s[i].x][s[i].y]=1;///表示以s[i].x为横坐标,s[i].y为纵坐标的点已经被搜索到 q.push(s[i]); while(!q.empty()) { temp=q.front(); q.pop(); x=temp.x; y=temp.y; if(mp[x][y]!=0 && mp[x][y]!=-1)///表示这个点数是A 或者 S { // cnt++; dis[i][mp[x][y]]=dis[mp[x][y]][i]=temp.step; /*if( cnt==dot) break;*/ } for(j=0; j<4; j++)///四个方向 { int xx=temp.x+dx[j]; int yy=temp.y+dy[j]; if(panduan(xx,yy)) { next.x=xx; next.y=yy; next.step=temp.step+1; vist[xx][yy]=1; q.push(next); } } } } } void prim() { int low[300],k,i,j,min1; int vis[300],sum; memset(vis,0,sizeof(vis)); for(i=1; i<=dot; i++) { low[i]= INF;///注意初始化 } sum=0; low[1]=0;///注意初始化 // vis[1]=1; for(i=1; i<=dot; i++) { min1=INF; for(j=1; j<=dot; j++) { if(low[j]<min1 && vis[j]==0) { min1=low[j]; k=j; } } sum+=min1; vis[k]=1; for(j=1; j<=dot; j++) { if(vis[j]==0&&low[j]>dis[k][j]) { low[j]=dis[k][j]; } } } printf("%d\n",sum); } int main() { int t,i,j; char c[60]; scanf("%d",&t); while(t--) { dot=0; scanf("%d%d",&n,&m); gets(c); for(i=1; i<=n; i++) { gets(str[i]); for(j=1; j<=m; j++) { if(str[i][j]=='A'||str[i][j]=='S')///把含有A S字符的存进结构体 { dot++; mp[i][j]=dot; ///方便bfs里面建距离图 mp[i][j]为距离图的横坐标 s[dot].x=i; s[dot].y=j; } else if(str[i][j]==' ') { mp[i][j]=0; } else if(str[i][j]=='#') { mp[i][j]=-1; } } } bfs(); prim(); } return 0; }