• 最小生成树+BFS J


    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

    2
    6 5
    ##### 
    #A#A##
    # # A#
    #S  ##
    ##### 
    7 7
    #####  
    #AAA###
    #    A#
    # S ###
    #     #
    #AAA###
    #####  
    

    Sample Output

    8
    11

    对所有S和A建立统统BFS一遍,建立邻接矩阵,然后Prim算法,这是第一次错误代码。没找出哪里错了。。。。
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<iomanip>
    #include<iostream>
    using namespace std;
    #define MAXN 101
    #define INF 0x3f3f3f3f
    int dis[MAXN][MAXN],lowcost[MAXN],m,n,tol,dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    char g[MAXN][MAXN];
    bool been[MAXN][MAXN];
    struct node
    {
        int x,y;
    };
    struct qnode
    {
        int x,y,step;
    };
    node a[MAXN];
    void BFS(int x,int y,int i)
    {
        memset(been,false,sizeof(been));
        qnode tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.step = 0;
        been[x][y] = true;
        queue<qnode> q;
        q.push(tmp);
        while(!q.empty())
        {
            tmp = q.front();
            q.pop();
            if(g[tmp.x][tmp.y]>='0'&&g[tmp.x][tmp.y]<='9')
            {
                dis[i][g[tmp.x][tmp.y]-'0'] = tmp.step;
            }
            for(int i=0;i<4;i++)
            {
                int tx = tmp.x+dir[i][0],ty = tmp.y+dir[i][1];
                if(tx>=0&&ty>=0&&tx<n&&ty<m&&g[tx][ty]!='#'&&!been[tx][ty])
                {
                    been[tx][ty] = true;
                    qnode n_node;
                    n_node.x = tx;
                    n_node.y = ty;
                    n_node.step = tmp.step+1;
                    q.push(n_node);
                }
            }
        }
    }
    int Prim()
    {
        int ret = 0;
        bool vis[MAXN];
        memset(vis,false,sizeof(vis));
        for(int i=1;i<tol;i++)
            lowcost[i] = dis[0][i];
        lowcost[0] = 0;
        vis[0] = true;
        for(int j=1;j<tol;j++)
        {
            int Minc = INF,k=-1;
            for(int i=0;i<tol;i++)
            {
                if(!vis[i]&&lowcost[i]<Minc)
                {
                    k = i;
                    Minc = lowcost[i];
                }
            }
            if(k==-1) return -1;
            vis[k] = true;
            ret+=Minc;
            for(int i=0;i<tol;i++)
            {
                if(!vis[i]&&dis[k][i]<lowcost[i])
                    lowcost[i] = dis[k][i];
            }
        }
        return ret;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            cin>>m>>n;//n行m列
            tol = 0;
            string str;
            for(int i=0;i<n;i++)
            {
                getline(cin,str);
                if(str.empty())
                {
                    i--;
                    continue;
                }
                for(int j=0;j<m;j++)
                {
                    g[i][j] = str[j];
                    if(str[j]=='A'||str[j]=='S')
                    {
                        g[i][j] = '0'+tol;
                        a[tol].x = i;
                        a[tol++].y = j;
                    }
                }
            }
            for(int i=0;i<tol;i++)
                for(int j=0;j<tol;j++)
                    dis[i][j] = INF;
            for(int i=0;i<tol;i++)
                BFS(a[i].x,a[i].y,i);
            int ans = Prim();
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    HRBUST 1377 金明的预算【DP】
    POJ 1745 Divisibility【DP】
    HRBUST 1476 教主们毕业设计排版==算法导论上的思考题整齐打印
    HRBUST 1220 过河【DP+状态】
    HRBUST 1478 最长公共子序列的最小字典序
    HRBUST 1162 魔女【DP】
    HDU 1561The more, The Better【DP】
    HRBUST 1376 能量项链【DP】
    POJ 1934 Trip【最长公共子序列输出】
    上传图片代码总结
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6591266.html
Copyright © 2020-2023  润新知