http://poj.org/problem?id=3026

题意:给你一个迷宫,里面有 ‘S’起点,‘A’标记,‘#’墙壁,‘ ’空地。求从S出发,经过所有A所需要的最短路。你有一个特殊能力,当走到S或A时可以分身出任意多个人一起走。计算路程时就是所有人的总路程之和。

题解:想一下,是裸的最短路套上bfs。

  先暴力bfs出各个点之间的距离,存边

  然后kruskal

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string.h>
#include<set>
#include<string>
using namespace std;
const int maxn = 3e4;;
int len1, len2, p[maxn], ans;
set<int> s;
struct edge {
int to, from, w;
edge(int to=, int from=, int w=) :to(to), from(from), w(w) {}
};
vector<edge> e;
bool cmp(edge a, edge b) {
return a.w < b.w;
}
int f[maxn];
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void un(int x, int y) {
int u = find(x), v= find(y);
f[u] = v;
}
bool same(int x, int y) {
return find(x) == find(y);
}
string map[];
int id[][];
int vis[][];
int dir[][] = { , ,,, ,-, -, };
struct node {
int x, y, t;
node(int x=, int y=, int t = ) :x(x), y(y), t(t) {}
};
void bfs(int x, int y) {
memset(vis, , sizeof(vis));
queue<node> Q;
Q.push(node(x, y, )); vis[x][y] = ;
while (!Q.empty()) {
node now = Q.front();
Q.pop();
for (int i = ; i < ; i++) {
int dx = now.x + dir[i][];
int dy = now.y + dir[i][];
if (map[dx][dy] == '#'||vis[dx][dy]) continue;
if (map[dx][dy] == 'A')e.push_back(edge(id[x][y], id[dx][dy], now.t+));
Q.push(node(dx, dy, now.t + ));
vis[dx][dy] = ; }
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
scanf("%d %d\n", &n,&m);
for (int i = ; i < m; i++) {
getline(cin, map[i]);
}
memset(id, , sizeof(id));
e.clear();
int idx = ;
for(int i=;i<m;i++)
for (int j = ; j < n; j++) {
if (map[i][j] == 'S')map[i][j] = 'A';
if (map[i][j] == 'A' ) {
id[i][j] = idx++;
}
}
for (int i = ; i<m; i++)
for (int j = ; j < n; j++) {
if(id[i][j])bfs(i, j);
}
for (int i = ; i < idx; i++) { f[i] = i; }
sort(e.begin(), e.end(),cmp);
int res = ;
for (int i = ; i < e.size(); i++) {
if (same(e[i].to, e[i].from)) continue;
un(e[i].to, e[i].from);
res += e[i].w;
}
cout << res << endl; }
system("pause");
}

POJ - 3026 Borg Maze bfs+最小生成树。的更多相关文章

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

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

  2. poj 3026 Borg Maze (BFS + Prim)

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

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

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

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

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

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

    题目链接:http://poj.org/problem?id=3026 题意:题意就是从起点开始可以分成多组总权值就是各组经过的路程,每次到达一个‘A'点可以继续分组,但是在路上不能分组 于是就是明显 ...

  6. poj 3026 Borg Maze bfs建图+最小生成树

    题目说从S开始,在S或者A的地方可以分裂前进. 想一想后发现就是求一颗最小生成树. 首先bfs预处理得到每两点之间的距离,我的程序用map做了一个映射,将每个点的坐标映射到1-n上,这样建图比较方便. ...

  7. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

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

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

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

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

随机推荐

  1. swoole的进程模型架构

    swoole的强大之处就在与其进程模型的设计,既解决了异步问题,又解决了并行. 主线程MainReactor swoole启动后主线程会负责监听server socket,如果有新的连接accept, ...

  2. trim思考

    今天发现后台订单商品名称没有的时候出现了HTML代码,然后看了一下源代码(下图是简化版本的) <?php $name = trim('<span style="font-weig ...

  3. moment.js用法总结

    moment(),获取当前时间 moment(string)把字符串变成moment时间格式 moment().format("YYYY-MM-DD HH:mm:ss"):规定时间 ...

  4. 【NodeJS】http-server.cmd

    npm install http-server @echo off  start cmd /k "D:\Program Files\nodejs\node_global\http-serve ...

  5. script 里写 html 模版

    js模版引擎(例如:template.js 或 handlebars.js)一般都用<script>标签来存放模版的内容 1)模版写在<script>标签和写在<div& ...

  6. iOS开发-NSDictionary

    判断一个字典中是否存在某个key,有两种方法: 方法一: if ([dictionary allKeys] containsObject: key]){ // cotains key operatio ...

  7. inux跟踪线程的方法:LWP和strace命令

    摘要:在使用多线程程序时,有时会遇到程序功能异常的情况,而这种异常情况并不是每次都发生,很难模拟出来.这时就需要运用在程序运行时跟踪线程的手段,而linux系统的LWP和strace命令正是这种技术手 ...

  8. 实现iOS中的链式编程

    谈到链式编程,那Masonry几乎就是最经典的代表.如: make.top.equalTo(self.view).offset() 像这样top.equalTo(self.view).offset(6 ...

  9. select选择option时触发的click事件google不兼容问题

    原先的方式,下面代码编写的问题在google浏览器下是触发不了click事件的,具体缘由不清楚,反正都可以概括为不兼容了 碰到问题时,百度到的一篇解决:http://blog.163.com/rihu ...

  10. ACER-4738ZG 拆机改散热

    前言 武汉真是个很热的地方,我的笔记本于2011年3月份左右购买的,到现在已经两年多了,第一个暑假,我是在苏州的空调房使用,第二个暑假,我是在实验室的空调房使用,没有直接感受到夏天对笔记本的杀伤力,今 ...