http://poj.org/problem;jsessionid=A0F3392F460475E0058F93E009D6BFC3?id=3026
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
8 11
Meaning of the questions:
Seeking the number of steps each point shortest letters are connected to each other together.
Ideas:
BFS + minimum spanning tree, beginning just do not know how handwriting
Since each of the S or A can split, so that one can walk and, seeking the path of least length, that is a tree containing all S or A point.
So, BFS enumerated first path length through each point to the other point, if the distance is less than the initial value, the distance is updated, and the establishment of such a complete graph, and this minimum spanning tree to complete a drawing.
Note that this problem may occur after a lot of space Oh input x, y! Will fail with the general% * c and getchar (), so we use gets (str), so WA several times.
According to "separation" rule meaning of the title, walked the streets no longer repeat the calculation
Therefore, when using prim algorithm for the length L, according to the characteristics of the algorithm just do not consider the question (source combined solves this problem), L is the total minimum spanning tree weight value W
Due to the use of prim algorithm for minimum spanning tree, so no matter what point do the starting point is the same, (usually select the first point), and therefore is not a starting point S does not matter.
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <sstream> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int mod=1e9+7; 16 //const double PI=acos(-1); 17 #define Bug cout<<"---------------------"<<endl 18 const int maxn=1e5+10; 19 using namespace std; 20 21 int NT[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; 22 char MP[110 ] [ 110 ]; // store labyrinth 23 is int G [ 110 ] [ 110 ]; // store FIG. 24 int Number [ 110 ] [ 110 ]; // store S or A maze number corresponding point 25 int lowval [ 110 ]; // auxiliary greedy array 26 is int VIS [ 110 ] [ 110 ]; // Analyzing maze when a location is passed BFS 27 int VIS [ 110 ]; // when Prim minimum spanning tree is determined whether to join the vertex 28 int n-, m; 29 30 struct node 31 { 32 int x; 33 int y; 34 int step; 35 }; 36 37 void BFS(int u,int x,int y) 38 { 39 memset(VIS,0,sizeof(VIS)); 40 node begins,next; 41 begins.x=x; 42 begins.y=y; 43 begins.step=0; 44 queue<node> qe; 45 qe.push(begins); 46 VIS[x][y]=1; 47 while(qe.size()) 48 { 49 node now=qe.front(); 50 qe.pop(); 51 if(MP[now.x][now.y]=='A'||MP[now.x][now.y]=='S') 52 G[u][number[now.x][now.y]]=now.step; 53 for(int i=0;i<4;i++) 54 { 55 next.x=now.x+NT[i][0]; 56 next.y=now.y+NT[i][1]; 57 if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&MP[next.x][next.y]!='#'&&!VIS[next.x][next.y]) 58 { 59 VIS[next.x][next.y]=1; 60 next.step=now.step+1; 61 qe.push(next); 62 } 63 } 64 } 65 } 66 67 int Prim(int N,int st) 68 { 69 memset(vis,0,sizeof(vis)); 70 int sum=0; 71 for(int i=0;i<N;i++) 72 lowval[i]=G[st][i]; 73 vis[st]=1; 74 for(int i=0;i<N-1;i++) 75 { 76 int t=-1; 77 int MIN=INF; 78 for(int j=0;j<N;j++) 79 { 80 if(!vis[j]&&lowval[j]<MIN) 81 { 82 MIN=lowval[j]; 83 t=j; 84 } 85 } 86 sum+=MIN; 87 vis[t]=1; 88 for(int j=0;j<N;j++) 89 { 90 if(!vis[j]&&lowval[j]>G[t][j]) 91 lowval[j]=G[t][j]; 92 } 93 } 94 return sum; 95 } 96 97 98 int main() 99 { 100 int T; 101 scanf("%d",&T); 102 while(T--) 103 { 104 Scanf ( " % D% D " , & n-, & m); // remember the last with a space or \ n filtered out in the input space 105 int K = 0 ; 106 for ( int I = 0 ; I <m; I ++ ) 107 { 108 the gets (the MP [I]); 109 for ( int J = 0 ; J <n-; J ++ ) 110 { 111 IF (the MP [I] [J] == ' A ' || the MP [I] [ J] == ' S ' ) 112 number[i][j]=k++; 113 } 114 } 115 for(int i=0;i<m;i++) 116 { 117 for(int j=0;j<n;j++) 118 { 119 if(i==j) 120 G[i][j]=0; 121 else G[i][j]=INF; 122 } 123 } 124 for(int i=0;i<m;i++) 125 { 126 for(int j=0;j<n;j++) 127 { 128 if(MP[i][j]=='A'||MP[i][j]=='S') 129 BFS(number[i][j],i,j); 130 } 131 } 132 printf("%d\n",Prim(k,0)); 133 } 134 }