问题: POJ3026

分析:

采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树。

AC代码:

 //Memory: 220K        Time: 32MS
 #include <iostream>
 #include <cstdio>
 #include <string>
 #include <cstring>
 #include <queue>

 using namespace std;

 ;
 ;
 char maze[maxn][maxn];
 int m[maxn][maxn];
 int g[max_alien][max_alien];
 int vis[maxn][maxn];
 ];
 ];
 int alien;
 int test, x, y;
 ], sj[max_alien + ];
 ][] = {{, }, {, -}, {, }, {-, }};

 struct node
 {
     int i;
     int j;
     int d;
     void set(int ii, int jj)
     {
         i = ii;
         j = jj;
         d = ;
     }
 }n1, n2;

 queue<struct node> q;

 void bfs(int num, int i, int j)
 {
     memset(vis, , sizeof(vis));
     int t = alien - num;
     while ( !q.empty() ) q.pop();
     n1.set(i, j);
     q.push(n1);

      ){
         n1 = q.front();
         q.pop();

         ; i < ; i++){
             n2.], n1.j + step[i][]);
             /*if (n2.i < 0 || n2.j < 0 ||)*/
             if (maze[n2.i][n2.j] == '#' || vis[n2.i][n2.j]) continue;

             vis[n2.i][n2.j] = ;
             n2.d = n1.d + ;

             if (m[n2.i][n2.j] > num){
                 t--;
                 g[num][ m[n2.i][n2.j] ] = g[ m[n2.i][n2.j] ][num] = n2.d;
             }

             q.push(n2);
         }
     }

 }

 int prim()
 {
     memset(v, , sizeof(v));
     v[] = ;
     ;
     ; i <= alien; i++)
         d[i] = g[][i];

     ; i < alien; i++) {

         , ix;
         ; j <= alien; j++) {
             if ( !v[j] && d[j] < _min){
                 _min = d[j];
                 ix = j;
             }
         }
         v[ix] = ;
         ret += d[ix];

         ; j <= alien; j++){
             if (!v[j] && d[j] > g[ix][j])
                 d[j] = g[ix][j];
         }
     }
     return ret;
 }

 int main()
 {
     scanf("%d", &test);
     while (test--){
         memset(m, , sizeof(m));
         scanf("%d%d", &x, &y);
         gets(maze[]);
         alien = ;
         ; i < y; i++)
             gets(maze[i]);
         ; i < y; i++){
             ; j < x; j++){
                 if (maze[i][j] == 'S'){
                     si[] = i;
                     sj[] = j;
                 }
                 else if (maze[i][j] == 'A') {
                     m[i][j] = ++alien;
                     si[alien] = i;
                     sj[alien] = j;
                 }
             }
         }

         memset(g, , sizeof(g));
         ; i <= alien; i++){
             bfs(i, si[i], sj[i]);
         }

         int ans = prim();

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

POJ3026 最小生成树的更多相关文章

  1. POJ-3026 Borg Maze---BFS预处理+最小生成树

    题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...

  2. POJ-3026(图上的最小生成树+prim算法+gets函数使用)

    Borg Maze POJ-3026 一开始看到这题是没有思路的,看了题解才知道和最小生成树有关系. 题目的意思是每次走到一个A或者S就可以分为多个部分继续进行搜索.这里就可以看出是从该点分出去的不同 ...

  3. POJ3026 Borg Maze(最小生成树)

    题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...

  4. POJ3026:Borg Maze (最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18644   Accepted: 5990 题目链接:h ...

  5. POJ3026 Borg Maze(bfs求边+最小生成树)

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

  6. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

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

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

  8. 最小生成树练习3(普里姆算法Prim)

    风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...

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

    https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...

随机推荐

  1. 织梦dedecms5.7后台进去就卡死解决方法

    症状:进入dede后台点击菜单后,浏览器进入假死状态要等好久才能反应过来. 解决方式:1.打开后台目录dede/templets/ 2.找到index_body.htm文件中的第25行至第35行部分屏 ...

  2. INI文件格式

    最近在看git命令,遇到INI文件格式,上网查了一下,把它总结一下: 程序没有任何配置文件,那么它对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,为了让程序出厂后还能根据需要 ...

  3. Linux中重命名文件

    linux下重命名文件有两种方式: 1.较简单的处理命令:mv mv 原文件名 新文件名 如:mv myFile newName 将MyFile重命名为newName. 2.linux提供了一个重命名 ...

  4. 【CCNA学习笔记】1.思科路由器的基本配置

    教学视频来源:http://edu.51cto.com/lesson/id-10930.html. 怎么安装模块.连交叉线什么的视频里面老师说的很清楚了,我只记录一下IOS配置的命令(虽然一副不明觉厉 ...

  5. hdu 1595 find the longest of the shortest

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 这道题我用spfa在枚举删除边的时候求最短路超时,改用dijkstra就过了. #include < ...

  6. BZOJ 2436 NOI嘉年华(单调优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2436 题意:两个会场不能同时表演,但是同一个时间可以同时表演,要求让两个会场表演数量最小的最大,然后 ...

  7. BZOJ 1497 最大获利(最大权闭合子图)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 思路:由题意可以得知,每个顾客都依赖2个中转站,那么让中转站连有向边到汇点,流量为它的建设费用 ...

  8. Windows 8.1 正式版镜像下载大全

    该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. [声明:所有资源均来自于网络,请购买正版授权后再使用.]Win8.1 正式版原版镜像 ...

  9. 在FMX中实现PostMessage的方法

    在FMX中实现PostMessage的方法 http://www.qdac.cc/?p=1090 2014-10-31 • Android.C++ Builder.Delphi.杂谈 • 4条评论 • ...

  10. 《Programming WPF》翻译 第7章 4.转换

    原文:<Programming WPF>翻译 第7章 4.转换 支持高分辨率显示是WPF中的重要样式.这是被部分地支持--强调了可伸缩的向量图,而不是图像.但是,正如使用GDI+和GDI3 ...