poj3026(bfs+prime)

Borg Maze
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.

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 letterS” 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

Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

果然英语还是太渣了。。开始题意没看懂。。我都会选择之间看输入输出。。结果发现好像搜索的地图题。。。果然 这题 题意是 S起点到每个A的距离为权值,输出最S到所有A短的最短路径,重点重复路径不算,也就是先用bfs遍历每个字母其他字母的距离,当做两个点之间的权值, 则可以构成一个 包含N个节点的无向完全连通图。再用prime求出最小生成树。虽然两个算法我最近都学到,不过结合起来 还是考验了我的代码能力。。。看了别人题解 也被一个地方卡住了 里面读回车不能用getchar()
…因为数据有bug, m*n后 有空格。。。日了dog

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Node
{
    int x;
    int y;
    int dis;
};
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}},n,m;
int M[300][300],dist[300],book[300][300],node[300][300],nodesum;//存邻接矩阵 ,node存,每个点的位置的同时,并为他们编号。nodesum,总共有多少个点。
char G[300][300];
void bfs(int tx,int ty)
{
    queue<Node>Q;
    while(!Q.empty()) Q.pop();
    memset(book,0,sizeof(book));
    book[tx][ty]=1;
    Node a;
    a.x=tx;
    a.y=ty;
    a.dis=0;
    Q.push(a);
    while(!Q.empty())
    {
        Node now=Q.front();
        Q.pop();
        if(node[now.x][now.y])
            M[node[a.x][a.y]][node[now.x][now.y]]=now.dis;
        for(int i=0;i<4;i++)
        {
            Node nxt;
            nxt.x=now.x+next[i][0];
            nxt.y=now.y+next[i][1];
            nxt.dis=now.dis+1;
            if(nxt.x<0||nxt.y<0||nxt.x>=n||nxt.y>=m)
                continue;
            if(book[nxt.x][nxt.y]==0&&G[nxt.x][nxt.y]!='#')
            {
                book[nxt.x][nxt.y]=1;
                Q.push(nxt);
            }
        }
    }
}
int boo[310];
int Prime()
{
    int sum=0;
    memset(boo,0,sizeof(boo));
    boo[0]=1;
    for(int i=0;i<nodesum;i++)
        dist[i]=M[0][i];
    for(int i=1;i<nodesum;i++)
    {
        int Min=99999999,index;
        for(int j=0;j<nodesum;j++)
        {
            if(!boo[j]&&dist[j]<Min)
            {
               Min=dist[j];
               index=j;
            }
        }
        boo[index]=1;
        sum+=Min;
        for(int j=0;j<nodesum;j++)
        {
            if(!boo[j]&&M[index][j]<dist[j])
                dist[j]=M[index][j];
        }
    }
    return sum;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        nodesum=0;
        scanf("%d %d",&m,&n);
        gets(G[0]);//读掉 空格回车的。。。注意不能用getchar()数据有bug
        memset(M,0,sizeof(M));
        memset(dist, 0, sizeof(dist));
        memset(node,-1,sizeof(node));
        for(int i=0;i<n;i++)
        {
            gets(G[i]);
            for(int j=0;j<m;j++)
            {
                if(G[i][j]=='S'||G[i][j]=='A')
                    node[i][j]=nodesum++;
            }
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
             if(node[i][j]!=-1)
                 bfs(i,j);

        printf("%d\n",Prime());
    }
}

你可能感兴趣的:(group,term,description,bfs-prime)