POJ3026 最小生成树
问题: 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 最小生成树的更多相关文章
- POJ-3026 Borg Maze---BFS预处理+最小生成树
题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S ...
- POJ-3026(图上的最小生成树+prim算法+gets函数使用)
Borg Maze POJ-3026 一开始看到这题是没有思路的,看了题解才知道和最小生成树有关系. 题目的意思是每次走到一个A或者S就可以分为多个部分继续进行搜索.这里就可以看出是从该点分出去的不同 ...
- POJ3026 Borg Maze(最小生成树)
题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...
- POJ3026:Borg Maze (最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18644 Accepted: 5990 题目链接:h ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- poj3026(bfs+prim)最小生成树
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- 最小生成树练习3(普里姆算法Prim)
风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...
- POJ - 3026 Borg Maze(最小生成树)
https://vjudge.net/problem/POJ-3026 题意 在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用最短的 ...
随机推荐
- Android应用中MVP最佳实践
转自:http://www.jianshu.com/p/ed2aa9546c2c 文/Jude95(简书作者)原文链接:http://www.jianshu.com/p/ed2aa9546c2c著作权 ...
- php array_combine 把array的默认键改成指定的字符串
array(2) { [0] => array(6) { [0] => string(1) "1" [1] => string(10) "1470650 ...
- PHP学习路上的一点心得
继学些了java后,接触php的项目后发现 php真的也是很强大的一门语言,这只是一篇回想,想到什么就写什么把,大家随便看看. 1.php其实无需等待,一般的改完代码后直接刷新页面即可,不需要像jav ...
- CSU 1335 高桥和低桥
开始队友说是线段树,看了看貌似也是,上手敲了个嵌套的线段树,O(nlognlogn)的复杂度果断tle了 TAT 思路:对h[i]排序,对每次涨水退水,先用二分查找,再用一个数组保存当前点之后所有点被 ...
- “假如花千骨在杭州拍摄” 主题Cosplay
“假如花千骨在杭州拍摄” 主题Cosplay 今天,2015年7月23日,本周三:此刻,现场正在中国杭州西湖举办“花千骨cosplay”大型分享活动,现场有超凡而孤高,冰凉而淡漠 ,温润如玉又云淡风清 ...
- initEvent vs initMouseEvent
var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, ...
- AOI
AOI(Automatic Optic Inspection)的全称是自动光学检测,是基于光学原理来对焊接生产中遇到的常见缺陷进行检测的设备.AOI是新兴起的一种新型测试技术,但发展迅速,很多厂家都推 ...
- Keil C51编译及连接技术
主要介绍Keil C51的预处理方法如宏定义.常用的预处理指令及文件包含指令,C51编译库的选择及代码优化原理,C51与汇编混合编程的方法与实现以及超过64KB空间的地址分页方法的C51实现. 教学目 ...
- DEVExpress For WPF 中GridControl如何实现滚动分页(延迟查询)
在显示大量数据时一般采用分页显示,但是最近用户需要滚动显示,那么问题来了,滚动显示要求将数据全部查询回来,这显然会导致显示速度很慢. 好在想到一种方式,就是当用户滚动鼓动条的时候再查询下面的数据.好吧 ...
- Php模板引擎Smarty安装和配置
Smarty 是PHP的一个模板引擎,是由Monte Ohrt 和 Andrei Zmievski 使用PHP语言开发的,发展至今已成为一个非常流行的模板引擎,Smarty 提供了一种易于管理和使用的 ...