zoukankan      html  css  js  c++  java
  • POJ 3026 Borg Maze (最小生成树)

    Borg Maze

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/124434#problem/I

    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

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

    Sample Output

    8 11
    ##题意: 题目真是难读,翻译一遍都没看懂... 大意就是要求最小距离把图中的所有A和S都联通.
    ##题解: 先用bfs处理出任意两点间的最短距离. 再求一遍最小生成树即可.
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 550 #define mod 100000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

    struct node{
    int left,right,cost;
    }road[maxn*maxn];

    int cmp(node x,node y) {return x.cost<y.cost;}
    int p[maxn*maxn],m,n;
    int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}
    int kruskal()
    {
    int ans=0;
    for(int i=1;i<=n;i++) p[i]=i;
    sort(road+1,road+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
    int x=find(road[i].left);
    int y=find(road[i].right);
    if(x!=y)
    {
    ans+=road[i].cost;
    p[x]=y;
    }
    }
    return ans;
    }

    void add_road(int u,int v,int w) {
    road[++m].left = u;
    road[m].right = v;
    road[m].cost = w;
    }

    int maps[maxn][maxn];

    struct Node{
    int x,y;
    int step;
    };

    bool vis[maxn][maxn];
    int dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
    void bfs(Node start)
    {
    queue q;
    while(!q.empty()) q.pop();
    memset(vis, 0, sizeof(vis));
    q.push(start); vis[start.x][start.y] = 1;

    while(!q.empty()) {
        Node cur = q.front(); q.pop();
        Node next;
        for(int i=0; i<4; i++) {
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            if(maps[next.x][next.y] == -1 || vis[next.x][next.y]) continue;
            vis[next.x][next.y] = 1;
            next.step = cur.step + 1;
            q.push(next);
            if(maps[next.x][next.y] == 0) continue;
            add_road(maps[start.x][start.y], maps[next.x][next.y], next.step);
        }
    }
    

    }

    int main(int argc, char const *argv[])
    {
    //IN;

    int t; cin >> t;
    while(t--)
    {
        m = 0; n = 0;
        memset(road,0,sizeof(road));
        int a,b; cin >> b >> a;
    
        memset(maps, -1, sizeof(maps));
        for(int i=1; i<=a; i++) { char tmp[maxn]; gets(tmp);
            for(int j=1; j<=b; j++) {
                char c; c = getchar();
                if(c == '#') maps[i][j] = -1;
                else if(c == ' ') maps[i][j] = 0;
                else maps[i][j] = ++n;
            }
        }
    
        for(int i=1; i<=a; i++)
        for(int j=1; j<=b; j++) if(maps[i][j] > 0){
            Node cur; cur.x = i; cur.y = j; cur.step = 0;
            bfs(cur);
        }
    
        int ans = kruskal();
    
        printf("%d
    ", ans);
    }
    
    return 0;
    

    }

  • 相关阅读:
    把IDEA中新建的项目提交到Github仓库中
    在IDEA中设置方法自动注释(带参数和返回值)
    如何在 Maven 工程中引入其他jar包 并生效?(以 Netty 为例)
    在 IDEA 中 配置 Maven
    Visio中锁定元件
    DevExpress中 TreeList控件的常规配置
    从SuperSocket的App.config中读取配置,并修改保存,再重启服务
    devexpress 中 XtraTabcontrol 改变BackColor 的方法
    DevExpress 中 GridControl 的数据源DataTable 内容改变后 重新绑定
    如何在linux中设置tab键长度
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5751022.html
Copyright © 2011-2022 走看看