poj-3026—bfs+最小生成树

来源:互联网 发布:云计算发展历史 编辑:程序博客网 时间:2024/06/05 16:51

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

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########  

Sample Output

811

题意:这道题的意思是给你一个图,图中有点为'A'或者‘S’,求让这些点联通的最小生成树;(其实因为题意不想读,我是让队友给我说的题意,嘿嘿)


这道题我们要先用bfs将每个点取出来建好图,再跑一遍prim就可以了

尼玛。。。我用邻接表RE,矩阵就过了。。。。卧槽

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define nn 209
#define inff 0x3fffffff
struct node
{
    int x,y,step;
};
int use[nn][nn];
char ma[nn][nn];
int hig,wid;
int id[nn][nn];
int dir[4][2]={ {1,0},
                {-1,0},
                {0,1},
                {0,-1},
              };
int sum_id;
/*struct point
{
    int en,next,len;
}road[nn];
int cnt;
int p[nn];
void init()
{
    cnt=0;
    memset(p,-1,sizeof(p));
}
void add(int s,int e,int le)
{
    road[cnt].en=e;
    road[cnt].len=le;
    road[cnt].next=p[s];
    p[s]=cnt++;
}
bool vist[nn*10];
int dis[nn*10];
int    prim()
{
    int i,j;
  //  int dis[nn*10];
    for(i=1;i<nn*10;i++)
        dis[i]=inff;
    dis[1]=0;
    int res=0;
    memset(vist,false,sizeof(vist));
    int idd;
    int w,k,h;
    int mm;
    for(i=1;i<sum_id;i++)
    {
        mm=inff;
        for(j=1;j<sum_id;j++)
        {
            if(vist[j]==false&&dis[j]<mm)
            {
                idd=j;
                mm=dis[j];
            }
        }
        vist[idd]=true;
        res+=dis[idd];


        for(j=p[idd];j!=-1;j=road[j].next)
        {
            w=road[j].en;
            dis[w]=min(dis[w],road[j].len);
        }
    }
return res;
}*/


int ss[nn][nn];
void init()
{
      int i,j;
    for(i=1;i<=nn;i++)
        for(j=1;j<=nn;j++)
            ss[i][j]=inff;
}
void add(int s,int e,int l)
{
    ss[s][e]=l;
  //  ss[e][s]=l;
}
bool vis[nn];
int dis[nn];
void prim()
{
int i,j,mink,MM,ans=0;
for(i=1;i<=nn;i++)
vis[i]=false,dis[i]=inff;//和DIJ算法作用一样
dis[1]=0;
for(i=1;i<sum_id;i++)
{
for(j=1,MM=inff;j<sum_id;j++)
if(!vis[j]&&dis[j]<=MM)//选出权值最小且两个点分别在U和S集合里的边
{
MM=dis[j];
mink=j;
}
vis[mink]=true;//放进S集合
ans+=dis[mink];
for(j=1;j<=sum_id;j++)
if(!vis[j]&&ss[mink][j]<dis[j])//松弛U集合的点到S集合的距离
dis[j]=ss[mink][j];
}
printf("%d\n",ans);//输出最小生成树的权值和
}
void bfs(int sx,int sy)
{
    queue<node> que;
    int i,j;
    int ty,tx;
    memset(use,0,sizeof(use));
    while(!que.empty())
        que.pop();
    node st,mid;
    st.x=sx;
    st.y=sy;
    st.step=0;
    que.push(st);
    use[sx][sy]=1;
    while(!que.empty())
    {
        mid=que.front();
        que.pop();
        if( ma[mid.x][mid.y]=='A'||ma[mid.x][mid.y]=='S')
        {


              add(id[sx][sy],id[mid.x][mid.y],mid.step);
             add(id[mid.x][mid.y],id[sx][sy],mid.step);
        }
         for(i=0;i<4;i++)
         {
             tx=mid.x+dir[i][0];
             ty=mid.y+dir[i][1];
             if(tx>=0&& tx<hig && ty>=0 && ty<wid && ma[tx][ty]!='#' && use[tx][ty]!=1)
             {
                 st.step=mid.step+1;
                 st.x=tx;
                 st.y=ty;
                 use[tx][ty]=1;
                 que.push(st);
             }
         }
    }
}
int main()
{
 int i,j;
    int T;
  while(~scanf("%d",&T))
  {
        while(T--)
        {
            scanf("%d %d",&wid,&hig);
            gets(ma[0]); //这里因为有空格 使用gets输入的,所以她会读入回车。所以我们要吃掉回车 
            for(i=0;i<hig;i++)
            {
                gets(ma[i]);
            }
            init();


            int num=1;
            for(i=0;i<hig;i++)
            {
                for(j=0;j<wid;j++)
                {
                    if(ma[i][j]=='A'||ma[i][j]=='S')
                    {
                        id[i][j]=num;
                        num++;
                    }
                }
            }
            sum_id=num;
            for(i=0;i<hig;i++)
            {
                for(j=0;j<wid;j++)
                {
                    if(ma[i][j]=='A'||ma[i][j]=='S')
                    {
                        bfs(i,j);
                    }
                }
            }
         prim();
        // printf("%d\n",prim());
       //  system("pause");
      }
  }
}


0 0
原创粉丝点击