在一个迷宫里面需要把一些字母。也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离,
**********************************************************************************
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; int  dir[][] = { {,},{,},{-,},{,-} };
char G[maxn][maxn];          //保存地图
int  D[maxn][maxn];          //记录两点间的距离
int  use[maxn][maxn];        //标记地图
int  Index[maxn][maxn];      //记录‘A’或者‘B’的编号
struct node{int x, y, step;}; void BFS(int k, int M,int N, int x, int y)
{
    queue<node> Q;
    node s;
    s.x = x, s.y = y, s.step = ;
    use[s.x][s.y] = k; Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();
        if(G[s.x][s.y]>='A' && G[s.x][s.y] <='Z')
            D[k][ Index[s.x][s.y] ] = s.step;         for(int i=; i<; i++)
        {
            node q = s;
            q.x += dir[i][], q.y += dir[i][];             if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && use[q.x][q.y]!=k)
            {
                use[q.x][q.y] = k;
                q.step += ;
                Q.push(q);
            }
        }
    }
}
int  Prim(int N)            //这里面的N代表编号最多到N
{
    int i, dist[maxn], vis[maxn]={, };
    int ans = , T=N-;     for(i=; i<=N; i++)
        dist[i] = D[][i];     while(T--)
    {
        int k=, mini = oo;         for(i=; i<=N; i++)
        {
            if(!vis[i] && mini > dist[i])
                mini = dist[i], k=i;
        }
        ans += mini;
        vis[k] = true;         for(i=; i<=N; i++)
            if(!vis[i])dist[i] = min(dist[i], D[k][i]);
    }     return ans;
} int main()
{
    int T;     scanf("%d", &T);     while(T--)
    {
        int i, j, M, N, t=;         scanf("%d%d ", &N, &M);         for(i=; i<M; i++)
        {
            gets(G[i]);
            for(j=; j<N; j++)
            {
                if(G[i][j]>='A' && G[i][j]<='Z')
                    Index[i][j] = t++;
                use[i][j] = ;
            }
        }         for(i=; i<M; i++)
        for(j=; j<=N; j++)
        {
            if(G[i][j]>='A' && G[i][j]<='Z')
                BFS(Index[i][j], M, N, i, j);
        }         int ans = Prim(t-);         printf("%d\n", ans);
    }     return ;
}

J - Borg Maze - poj 3026(BFS+prim)的更多相关文章

  1. POJ 3026(BFS+prim)

    http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...

  2. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  4. (最小生成树) Borg Maze -- POJ -- 3026

    链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  5. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  6. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

  7. Borg Maze poj 3026

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  8. poj3026(bfs+prim)

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

  9. poj3026(bfs+prim)最小生成树

    The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...

随机推荐

  1. RHEL7下PXE+Apache+Kickstart无人值守安装操作系统

    RHEL7下PXE+Apache+Kickstart无人值守安装操作系统 1.配置yum源 vim /etc/yum.repos.d/development.repo [development] na ...

  2. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. sql的一些小东西

    1.sa账户密码丢失. 先用wiondows验证登陆,然后新建查询 “ ALTER LOGIN [sa] WITH PASSWORD = N'NewPassword' ”

  4. css.day02

    1.复合选择器 复合选择器 一般会有几个标签混合使用 .把多个组合成一个  称之为复合 1.1标签指定式 复合选择器 (交集选择器) 格式: 标记选择器+ 类名/ID名称 { 属性:值;} 实际情况用 ...

  5. ASP.NET5 静态文件

    静态文件,包括HTML文件,CSS文件,图像文件和JavaScript文件,它是一个应用里所包含的资源. 1. 提供静态文件 默认的,静态文件存储在你的webroot目录下面,webroot的路径定义 ...

  6. Set Keep-Alive Values---C到C#代码的转换

    一.什么是Keep-Alive模式? 我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成 之后立即断开连接(HT ...

  7. MySQL user表root用户误删除后恢复

    mysql user表root 用户误删除后恢复root用户 方法/步骤 1.停止mysql服务:在mysql安装目录下找到my.ini:在my.ini中找到以下片段[mysqld]:另起一行加入代码 ...

  8. 汇编程序w=x*y+z-200

    DATA   SEGMENTX      DW  1000Y      DW  2000Z      DW  3000W      DW  2 DUP(?)DATA   ENDSCODE   SEGM ...

  9. 【USACO 1.3.1】混合牛奶

    [题目描述] 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助梅丽乳业找到最优的牛奶采购方案. 梅丽乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的. ...

  10. 2.2.4 转换 Path

    在NIO.2里可以很容易地合并Path,在两个Path中再创建Path或对Path进行比较: Demo: import java.nio.file.Path; import java.nio.file ...