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
题意:从s点起经过每个a点的最小距离
题解:bfs找到所有字母到其余字母的距离,再跑一遍最小生成树
但是…一直wa
代码:
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int MAXN=110;//最大点数 const int MAXM=10005;//最大边数 int F[MAXN];//并查集使用 struct Edge { int u,v,w; }edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0 void addedge(int u,int v,int w) { edge[tol].u=u; edge[tol].v=v; edge[tol++].w=w; } bool cmp(Edge a,Edge b) {//排序函数,讲边按照权值从小到大排序 return a.w<b.w; } int find(int x) { if(F[x]==-1)return x; else return F[x]=find(F[x]); } int Kruskal(int n)//传入点数,返回最小生成树的权值,如果不连通返回-1 { memset(F,-1,sizeof(F)); sort(edge,edge+tol,cmp); int cnt=0;//计算加入的边数 int ans=0; for(int i=0;i<tol;i++) { int u=edge[i].u; int v=edge[i].v; int w=edge[i].w; int t1=find(u); int t2=find(v); if(t1!=t2) { ans+=w; F[t1]=t2; cnt++; } if(cnt==n-1)break; } if(cnt<n-1)return -1;//不连通 else return ans; } char c[MAXN][MAXN]; bool vis[MAXN][MAXN]; map<int,int>ma; struct node{ int x; int y; int cnt; node(int _x=0,int _y=0,int _cnt=0):x(_x),y(_y),cnt(_cnt){} }; int dir[4][2]={0,-1,0,1,-1,0,1,0}; int n,m; bool ok(int x,int y){ if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&c[x][y]!='#')return 1; else return 0; } void bfs(int x,int y){ CLR(vis); queue<node>q; while(!q.empty())q.pop(); q.push(node(x,y,0)); vis[x][y]=1; while(!q.empty()){ node t=q.front(); q.pop(); int xx=t.x,yy=t.y,cnt=t.cnt; rep(i,0,3){ int tx=xx+dir[i][0],ty=yy+dir[i][1]; if(ok(tx,ty)){ vis[tx][ty]=1; q.push(node(tx,ty,cnt+1)); if((c[tx][ty]=='A'||c[tx][ty]=='S')&&ma[x*1e4+y]<ma[tx*1e4+ty]){ addedge(ma[x*1e4+y],ma[tx*1e4+ty],cnt+1);} } } } } int main(){ int t; scanf("%d", &t); while(t--){ tol=0; scanf("%d %d", &m,&n); int cnt=0; // ma.clear(); getchar(); rep(i,1,n) { gets(c[i]+1); rep(j,1,m) { if(c[i][j]=='A' || c[i][j]=='S') ma[1e4*i+j]=cnt++;//给每个点标号 } } // rep(i,1,n){ // getchar(); // rep(j,1,m){ // c[i][j]=getchar(); // if(c[i][j]=='S'||c[i][j]=='A'){ // ma[i*1e4+j]=++cnt; // } // } // } rep(i,1,n) rep(j,1,m){ if(c[i][j]=='A'||c[i][j]=='S'){ bfs(i,j); } } printf("%d", Kruskal(cnt)); if(t)printf("\n"); } return 0; } /* 2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### ##### */