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出发,要求用最短的 ...
随机推荐
- 添加AdMob 错误记录
依照官方教程添加文件及其 frameWork后 发现运行报错 错误如下 Undefined symbols for architecture i386: "_OBJC_CLASS_$_ASI ...
- 最长公共子序列--nyoj36
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- c#添加事物(全部执行和带保存点的执行)
全部执行 protected void Button2_Click(object sender, EventArgs e) { // 执行事务 SqlConnection con = new SqlC ...
- MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
在看深入JVM虚拟机一书(p50,2.4 实战OutOfMemoryError),有一个Java堆溢出的例子,使用到了Eclipse Memory Analyzer插件,由于自己现在使用的是MyEcl ...
- CoreData学习-最好的一片文章
CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么 ...
- FMDB官方使用文档-GCD的使用-提高性能(翻译)
FMDB官方使用文档-GCD的使用-提高性能(翻译) 发布于:2013-08-19 10:01阅读数:13395 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此 ...
- [UVA] 784 - Maze Exploration
Maze Exploration A maze of rectangular rooms is represented on a two dimensional grid as illustra ...
- [Codeforces Round #186 (Div. 2)] A. Ilya and Bank Account
A. Ilya and Bank Account time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- qemu cow镜像分析
最近研究了以下qemu最简单的read on direct 镜像格式cow,在稀疏文件的模式下,这种方式还是比较简单,而且有优势的.其优势主要体现在云计算环境中,不需要用到qcow2的那些诸如内部快照 ...
- poj2505-A multplication game
题意:两个人轮流用2~9来乘n,使n不断扩大,n开始为1.当给一个固定值k,谁先使n超过k谁赢. 分析:能到达必败态的状态为必胜态,只能到达必胜态的状态为必败态.对于给定的k,n>=k时为必败态 ...