poj 3026( Borg Maze BFS + Prim)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/E
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### #####
恶心的题目。
题意: 给出 一个m*n的迷宫,空格代表路径,#代表墙, 现在求 从S到达所有A的最短路径和, 重复的路径不计算;
思路: 将 所有A和S 看做 节点, 则可以构成一个 包含N个节点的无向完全连通图, 权值 为 字母到其他字母的 距离;BFS求出字母到字母的距离;
则 问题可看做 求 改图的最小生成树;利用Prim求解即可;
注意 数据有bug, m*n后 有空格 所以 不能用getchar();
WA了N 遍,爽啊。
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #define N 100001 using namespace std; int n,m,tt; char map[1001][1001]; int v[1001][1001]; int a[1001][1001]; int v1[1001],dis[1001]; struct node { int x,y,ans; }; int m1[1001][1001]; int jx[]={1,-1,0,0}; int jy[]={0,0,-1,1}; void bfs(int x,int y) { int i; queue<node>q; memset(v,0,sizeof(v)); struct node t; t.x =x; t.y =y; v[t.x][t.y] = 1; t.ans = 0; q.push(t); while(!q.empty()) { struct node f = q.front(); int tx = f.x; int ty = f.y; int tans = f.ans; q.pop(); if(map[tx][ty]=='S'||map[tx][ty]=='A') { m1[a[x][y]][a[tx][ty]] = tans;//将A或S点转化为1~n的图。 } for(i = 0; i <= 3 ;i++) { int ti = jx[i]+tx; int tj = jy[i]+ty; if(ti>=1&&ti<=n&&tj>=1&&tj<=m&&v[ti][tj]==0&&map[ti][tj]!='#') { v[ti][tj] = 1; t.x = ti; t.y = tj; t.ans = tans+1; q.push(t); } } } } void Prime(int dd) { int i,j,k,sum=0,max; memset(v1,0,sizeof(v1)); for(i = 1 ;i <= dd ; i++) dis[i] = m1[1][i]; v1[1] = 1; for(i = 1 ; i <dd ; i++) { max = N; for(j = 1 ; j <= dd ; j++) if(!v1[j]&&max>dis[j]) max = dis[k=j]; v1[k] = 1; sum+=max; for(j = 1 ; j <= dd ; j++) if(!v1[j]&&dis[j]>m1[k][j]) dis[j] = m1[k][j]; } cout<<sum<<endl; } int main() { int T,i,j; char ch[101]; scanf("%d",&T); while(T--) { tt=0; cin>>m>>n; gets(ch); for(i = 1;i<=n; i++)//用scanf("%s",map[i])输不进去。 { getchar(); for(j = 1; j <= m ; j++) { scanf("%c",&map[i][j]); if(map[i][j]=='A'||map[i][j]=='S') { tt++; a[i][j]=tt;//将点A或点S转化为1~n的节点。 } } } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { m1[i][j]=N; m1[j][i]=N; } m1[i][i]=0; } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(map[i][j]=='A'||map[i][j]=='S') { bfs(i,j); } } } Prime(tt); } return 0; }