题意:

求把S和所有的A连贯起来所用的线的最短长度。。。

这道题。。不看discuss我能wa一辈子。。。

输入有坑。。。

然后,,,也没什么了。。。还有注意 一次bfs是可以求当前点到所有点最短距离的。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int f[maxn], d[][];
int cnt, n, m;
int dis[][] = {{,},{-,},{,},{,-}};
char str[][], temp[maxn];
struct node
{
int u, v, w;
}Node[maxn]; struct edge
{
int x, y;
}Edge[maxn]; void add(int u, int v, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt++].w = w;
} void init()
{
for(int i=; i<maxn; i++) f[i] = i;
cnt = ;
} int cmp(node a, node b)
{
return a.w < b.w;
} int find(int x)
{
// return f[x]==x?x:(f[x] = find(f[x]));
if(f[x] == x) return x;
int r = f[x];
while(r != f[r]) r = f[r];
int i = x, j;
while(i!=r)
{
j = f[i];
f[i] = r;
i = j;
}
return r;
} void bfs(edge s)
{
queue<edge> Q;
mem(d, );
Q.push(s);
d[s.x][s.y] = ;
while(!Q.empty())
{
s = Q.front(); Q.pop();
for(int i=; i<; i++)
{
edge t;
t.x = s.x + dis[i][];
t.y = s.y + dis[i][];
if(t.x < || t.x >= n || t.y < || t.y >= m || d[t.x][t.y] || str[t.x][t.y] == '#') continue;
d[t.x][t.y] = d[s.x][s.y] + ;
Q.push(t);
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int ans = ;
scanf("%d%d",&m,&n);
gets(temp);
// getchar();
for(int i=; i<n; i++)
{
gets(str[i]);
for(int j=; j<m; j++)
{
if(str[i][j] == 'A' || str[i][j] == 'S')
Edge[++ans].x = i, Edge[ans].y = j;
}
}
for(int i=; i<=ans; i++)
{
bfs(Edge[i]);
for(int j=i+; j<=ans; j++)
add(i, j, d[Edge[j].x][Edge[j].y] - ); }
sort(Node, Node+cnt, cmp);
int sum = ;
for(int i=; i<cnt; i++)
{
int r = find(Node[i].u);
int l = find(Node[i].v);
if(r == l) continue;
f[r] = l;
sum += Node[i].w;
} printf("%d\n",sum); } return ;
}

Borg Maze POJ - 3026 (BFS + 最小生成树)的更多相关文章

  1. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  2. poj 3026(BFS+最小生成树)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12032   Accepted: 3932 Descri ...

  3. POJ 3026 Borg Maze 广搜(BFS)+最小生成树

    题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...

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

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

  5. J - Borg Maze - poj 3026(BFS+prim)

    在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...

  6. Borg Maze poj 3026

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

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  9. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. 关于for,while,dowhile效率测试

    引言 大家都知道每种循环对应的效率是不同的,书中都说在循环中使用减法的效率是比加法的效率高的,具体情况是怎么样,我们将详细列出各循环的执行效率问题.本文通过查看汇编代码比较各循环的效率以及i++,++ ...

  2. maven 插

    一.maven插件元素 <?xml version="1.0" encoding="utf-8"?> <plugin> <!--插 ...

  3. XML转换为对象/Javabean

    將XML轉換為對象 /** * xml转换成JavaBean * @param xml * @param c * @return */ public static <T> T conver ...

  4. img图片加载出错处理(转载)

    为了美观当网页图片不存在时不显示叉叉图片当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验.即使使用alt属性给出了”图片XX”的提示信息,也起不了 ...

  5. Ionic 安装JPush过程

    1.在官网注册App帐号,完成后会生成对应的AppKey 2. 进行在线安装 cordova plugin add https://github.com/jpush/jpush-phonegap-pl ...

  6. [Partition][Index]对于Partition表而言,是否Global Index 和 Local Index 可以针对同一个字段建立?

    对于Partition表而言,是否Global Index 和 Local Index 可以针对同一个字段建立? 实验证明,对单独的列而言,要么建立 Global Index, 要么建立 Local ...

  7. .NET日志记录之——log4net划重点篇

    .NET日志记录之--log4net划重点篇 1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不 ...

  8. System.Data.SqlClient.SqlException:“对象名 'customer' 无效。"

    连接数据库出错, 错误原因:表名错误.

  9. 为小米(红米)6A解锁_ROOT_安装天下游虚拟定位教程_已亲身验证通过!附图

    第一步骤: 把TeamViewer发给客户 第二步骤: 在自己电脑上购买小米账号:http://shop1.91kami.com/UXWHTEY9KN?cid=1200 要求:(购买邮箱注册账号,需要 ...

  10. Python进阶量化交易专栏场外篇7- 装饰器计算代码时间

    欢迎大家订阅<教你用 Python 进阶量化交易>专栏!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外已陆续推出一些手记来辅助同学们学习本专栏内容,目前已推出如下扩展篇: 在第一篇 ...