题目链接

题目大意:

任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小。

分析:

第一感觉使3维的BFS。但写着写着,发现不对。

应当用最小生成树解法。把每个字母(即A,或S)看成一个结点,如果求出来任意两个结点间的权值,则求解即为求最小生成树。

通过暴力,对每一个字母进行BFS,求出任意两个结点的距离。然后prim.

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue> using namespace std; const int maxn = ;
const int INF = (<<); int dx[] = {, , -, };
int dy[] = {, -, , }; struct Pos {
int x, y;
}; char G[maxn][maxn]; //存放地图
bool vis[maxn][maxn]; //深搜时用的标记数组
int num[maxn][maxn], node[][]; //num表示字母编号(从0开始),node字母间的最短距离
int dis[maxn][maxn]; //深搜时用来记录到(sx,sy)的最短距离
int n, m, cn; void BFS(int sx, int sy) {
queue<Pos> Q; memset(vis, , sizeof(vis)); Q.push((Pos){sx, sy});
vis[sx][sy] = true;
dis[sx][sy] = ; while(!Q.empty()) {
Pos e = Q.front(); Q.pop();
int x = e.x, y = e.y; if(num[x][y] != -) //(x,y)这点是字母
node[num[sx][sy]][num[x][y]] = dis[x][y]; for(int d=; d<; d++) {
int nx = x+dx[d];
int ny = y+dy[d]; if(nx < && ny < && nx >= n && ny >= m) continue;
if(vis[nx][ny] || G[nx][ny] == '#') continue; vis[nx][ny] = true;
dis[nx][ny] = dis[x][y]+;
Q.push((Pos){nx, ny});
}
}
} int prim(int s) {
/*
* cn为结点数,node为邻接矩阵
* 任意两点间距离node[i][j]
* 本题就是prim模板
*/ int d[], ans = ;
bool v[]; memset(v, false, sizeof(v)); for(int i=; i<cn; i++) {
d[i] = node[s][i];
} v[s] = true;
d[s] = ; for(int i=; i<cn-; i++) {
int x, m=INF;
for(int y=; y<cn; y++) if(!v[y] && m >= d[y]) m = d[x=y];
v[x] = true;
ans += m;
for(int y=; y<cn; y++) if(!v[y] && d[y] > node[x][y]) d[y] = node[x][y];
} return ans;
} int main() {
int T, n, m, ns;
char tmp[]; scanf("%d", &T); while(T--) {
scanf("%d%d", &m, &n);
gets(tmp); //discuss上说后面有多个空格
for(int i=; i<n; i++) {
gets(G[i]);
} cn = ; //字母的个数
memset(num, -, sizeof(num)); for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(G[i][j] == 'S' || G[i][j] == 'A') { //对字母进行编号
num[i][j] = cn++;
} if(G[i][j] == 'S') { //'S'的编号
ns = cn-;
}
}
} for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
if(G[i][j] == 'A' || G[i][j] == 'S')
BFS(i, j);
}
} printf("%d\n", prim(ns));
} return ;
}

POJ3026 Borg Maze(最小生成树)的更多相关文章

  1. POJ3026:Borg Maze (最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18644   Accepted: 5990 题目链接:h ...

  2. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

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

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

  4. POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14165   Accepted: 4619 Descri ...

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

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

  6. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  7. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

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

    题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...

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

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

随机推荐

  1. 山东省赛J题:Contest Print Server

    Description In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source c ...

  2. 小胖学PHP总结1-----PHP的数据类型

    PHP一共支持8种原始类型.包含4中标量类型,即:boolean(布尔型).integer(整形).float/double(浮点型)和string(字符串型):两种复合类型,即:array(数组)和 ...

  3. [ES6] Array.find()

    Convenient method to find one item in an array, avoid writing and  for + if: let arys = [1,,5,,6] ; ...

  4. Linux 内核的文件 Cache 管理机制介绍-ibm

    https://www.ibm.com/developerworks/cn/linux/l-cache/ 1 前言 自从诞生以来,Linux 就被不断完善和普及,目前它已经成为主流通用操作系统之一,使 ...

  5. Java EE 7 / JAX-RS 2.0: Simple REST API Authentication & Authorization with Custom HTTP Header--reference

    REST has made a lot of conveniences when it comes to implementing web services with the already avai ...

  6. Dagger2学习之由浅入深

    概述 Dagger2是一款使用在Java和Android上的静态的,运行时依赖注入框架.官方地址:http://google.github.io/dagger/ 记得当初刚学习Dagger2的时候看了 ...

  7. 2015 南阳ccpc The Battle of Chibi (uestc 1217)

    题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...

  8. 洛谷 1503 鬼子进村 (set)

    /*set加速维护*/ #include<iostream> #include<cstdio> #include<cstring> #include<set& ...

  9. DataSet离线数据集实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. 转载:浅析C#深拷贝与浅拷贝

    原文地址 :http://www.cnblogs.com/xugang/archive/2010/09/09/1822555.html   感谢博主分享! 也许会有人这样解释C# 中浅拷贝与深拷贝区别 ...