题目链接:http://poj.org/problem?id=3026

题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组

于是就是明显的最小生成树,点与点的距离要用bfs或者dfs求一下。这题bfs要稍微优化一下。不能下暴力的bfs就是两点两点之间

bfs这样会有很多重复的查询会超时,所以要一次性的bfs找到一个点就bfs到底。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int x , y , num , pos[200][200] , cost[200][200] , f[200] , dr[4][2] = {0 , 1 , 0 , -1 , 1 , 0 , -1 , 0};
char s[60][60] , cp[10];
void init() {
for(int i = 0 ; i <= num ; i++) {
f[i] = i;
}
}
int find(int x) {
if(x == f[x])
return x;
int tmp = find(f[x]);
return f[x] = tmp;
}
struct TnT {
int x , y , step;
};
struct node {
int x , y , val;
}nodes[40000];
bool cmp(node a , node b) {
return a.val < b.val;
}
bool vis[110][110] , have[110][110];
void bfs(int xx , int yy) {
memset(vis , false , sizeof(vis));
queue<TnT>q;
vis[xx][yy] = true;
TnT gg , gl;
gg.x = xx , gg.y = yy , gg.step = 0;
q.push(gg);
while(!q.empty()) {
gg = q.front();
q.pop();
if(pos[gg.x][gg.y] != -1) {
cost[pos[xx][yy]][pos[gg.x][gg.y]] = gg.step;
}
for(int i = 0 ; i < 4 ; i++) {
gl.x = gg.x + dr[i][0];
gl.y = gg.y + dr[i][1];
gl.step = gg.step + 1;
if(gl.x >= 0 && gl.x < y && gl.y >= 0 && gl.y < x && s[gl.x][gl.y] != '#' && !vis[gl.x][gl.y]) {
vis[gl.x][gl.y] = true;
q.push(gl);
}
}
}
}
int main() {
int t;
scanf("%d" , &t);
while(t--) {
scanf("%d%d" , &x , &y);
gets(cp);
for(int i = 0 ; i < y ; i++) {
for(int j = 0 ; j < x ; j++) {
pos[i][j] = -1;
cost[i][j] = -1;
}
}
num = 0;
for(int i = 0 ; i < y ; i++) {
gets(s[i]);
for(int j = 0 ; j < x ; j++) {
if(s[i][j] == 'A' || s[i][j] == 'S') {
pos[i][j] = num++;
}
}
}
for(int i = 0 ; i < y ; i++) {
for(int j = 0 ; j < x ; j++) {
if(s[i][j] == 'A' || s[i][j] == 'S') {
bfs(i , j);
}
}
}
init();
int count = 0;
for(int i = 0 ; i < num ; i++) {
for(int j = i + 1 ; j < num ; j++) {
if(cost[i][j] != -1) {
nodes[count].x = i , nodes[count].y = j , nodes[count].val = cost[i][j];
count++;
}
}
}
sort(nodes , nodes + count , cmp);
int sum = 0 , temp = 0;
for(int i = 0 ; i < count ; i++) {
int a = find(nodes[i].x) , b = find(nodes[i].y);
if(a != b) {
f[a] = b;
sum += nodes[i].val;
temp++;
}
if(temp == num - 1)
break;
}
printf("%d\n" , sum);
}
return 0;
}

poj 3026 Borg Maze(最小生成树+bfs)的更多相关文章

  1. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  2. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

  3. POJ 3026 Borg Maze【BFS+最小生成树】

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

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

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6634   Accepted: 2240 Descrip ...

  5. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

  6. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  7. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  8. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

  9. poj 3026 Borg Maze (bfs + 最小生成树)

    链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走).空格代表空位(可走).S代表搜索起点(可走),A代表目的地(可走),如今要从S出发,每次可上下左右移动一格到可走的地方.求到达全 ...

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

    Borg Maze 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/I Description The Borg is an im ...

随机推荐

  1. Android平台使用Ceres Solver

    在Android平台上使用Ceres求解器,官方教程不明确,且编译过程遇到了很多问题. 环境 Ubuntu 18.04 源代码 https://github.com/Great-Keith/ceres ...

  2. Linux基础文件查找

    一.文件查找 (一).命令文件 [root@linux ~]# chich ls //从PATH环境变量 [root@linux ~]# chereis vim [root@linux ~]# ech ...

  3. python基础之变量与数据类型

    变量在python中变量可以理解为在计算机内存中命名的一个存储空间,可以存储任意类型的数据.变量命名变量名可以使用英文.数字和_命名,且不能用数字开头使用赋值运算符等号“=”用来给变量赋值.变量赋值等 ...

  4. python使用pip安装第三方库以及镜像使用豆瓣源安装第三方库

    2018/8/7  在使用pip安装pynum第三方库时的随笔 所有的前提都是你成功安装了pip 首先第一步 打开命令提示符  输入pip show pip 查看当前pip版本 然后可以上官网搜索一下 ...

  5. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  6. Kafka面试,看这篇文章就够了

    原文链接:https://mp.weixin.qq.com/s/zxPz_aFEMrshApZQ727h4g** 引言 MQ(消息队列)是跨进程通信的方式之一,可理解为异步rpc,上游系统对调用结果的 ...

  7. javaScript基础-0 javascript概述

    一.简介 javaScript一种面向web的编程语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早 ...

  8. js及jquery常用代码

    1.获取屏幕尺寸 document.documentElement.scrollWidth; document.documentElement.scrollHeight; $(window).widt ...

  9. java后端_百度二面

    参考: https://www.nowcoder.com/discuss/215891?type=2&order=0&pos=10&page=1 1. gc 2. java l ...

  10. Go语言框架:Beego vs Gin 的区别

    前言: 一切语言.技术或者框架,本质都是工具,工具的价值在于为使用者提供竞争优势. 一.Beego和Gin全方位比较 MVC Beego支持完整的MVC, Gin不支持完整的MVC(需要开发者自己实现 ...