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出发,要求用最短的 ...
随机推荐
- [GitPython]使用python管理你的git库
你想使用python来自动管理一个git仓库的时候,GitPython是一个不错的选择. 简介: 先看看下面这段代码: from git import Repo repo = Repo("~ ...
- JAVA语言对比C++语言的几个优点和自身的关键特性
之所以写这篇博客不是为了比较JAVA语言和C++语言到底孰优孰劣,而是从语言本身出发进行对比总结程序编码设计上的差别.因水平有限,某些内容可能太过片面,欢迎指正和讨论. JAVA语言在设计上很好地借鉴 ...
- VBoxManage.exe: error: Resize hard disk operation for this format is not implemented yet!
VirtualBox虚拟磁盘空间不够了,默认10G.想扩大,图形界面下没有找到可操作菜单.Google了一下用 Vbox自带的命令工具VBoxManage即可解决. C:\Program Files\ ...
- C++基本要点复习--------coursera程序设计实习(PKU)的lecture notes
因为一些特性复杂,很多时候也用不到一些特性,所以忘记了,算是随笔,也当作一个临时查找的手册.没有什么顺序,很杂. 1.构造函数通过函数重载的机制可以有多个(不同的构造函数,参数个数,或者参数类型不同. ...
- Eclipse-cdt 配合 gdbserver 进行 arm 程序远程调试 上
做嵌入式Linux开发也不用再羡慕windows程序员VS集成开发环境的强大,我们同样能够搭建出给力的IDE. 今天在这里记录一下我使用Eclipse-cdt,gdb,gdbserver搭建远程arm ...
- C++初始化列表问题
C++初始化列表问题,类中有一个对象类型的数组成员变量,在初始化列表中初始化时报错“[]”操作符语法错误 class ClassA{private ClassB arrayOfObjectClassB ...
- NOI2012 Day1
NOI2012 Day1 随机数生成器 题目描述:给出数列\(X_{n+1}=(aX_n+c)mod m\),求\(X_n mod g\) solution: 矩阵乘法,但数有可能在运算时爆\(lon ...
- c#提出中文首字母
; i < len; i++) { myStr += getSpell(strText.Substring(i, )); ...
- Linux权限机制
权限是操作系统用来限制用户.组.进程对操作系统资源(文件.设备等)的访问的机制 权限分为:读.写.执行,一般表示为 r.w.x http://itercast.com/lecture/22 每个文件或 ...
- Paint House 解答
Question There are a row of n houses, each house can be painted with one of the three colors: red, b ...