POJ3026:Borg Maze (最小生成树)
Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18644 | Accepted: 5990 |
题目链接:http://poj.org/problem?id=3026
Description:
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.
Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.
Input:
On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.
Output:
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
Sample Input:
- 2
- 6 5
- #####
- #A#A##
- # # A#
- #S ##
- #####
- 7 7
- #####
- #AAA###
- # A#
- # S ###
- # #
- #AAA###
- #####
Sample Output:
- 8
- 11
题意:
从S点出发,目的是要到达所有外星人的位置。然后在起点或者有外星人的点,可以进行“分组”,也就是进行多条路径搜索。
最后的总代价定义为所有组行走的步数之和。比如这个组一开始走了5步,然后分为两组,各走三步,最终总代价为11步。
问的就是所有外星人位置被到达后的最小总代价为多少。
题解:
这个看似是搜索...也很像是搜索。如果不是在最小生成树专题里面,我估计也不会想到最小生成树。
其实这个题如果能够想到最小生成树就简单了,最终的目的转化为S以及所有的A都连通嘛,并且最小代价。
那么就直接先bfs求出两点间的最短距离,然后根据距离信息建图,跑最小生成树就行了。
代码如下:
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #include <queue>
- #define INF 0x3f3f3f3f
- using namespace std;
- typedef long long ll;
- const int N = ;
- int t,n,m;
- int num[N][N],d[N][N];
- char mp[N][N];
- int dx[]={,-,,},dy[]={,,,-};
- struct Edge{
- int u,v,w;
- bool operator < (const Edge &A)const{
- return w<A.w;
- }
- }e[N*N<<];
- struct node{
- int x,y;
- };
- int f[N*N];
- int tot,cnt;
- int find(int x){
- return f[x]==x?f[x]:f[x]=find(f[x]);
- }
- int Kruskal(){
- int ans=;
- for(int i=;i<=;i++) f[i]=i;
- for(int i=;i<=tot;i++){
- int fx=find(e[i].u),fy=find(e[i].v);
- if(fx==fy) continue ;
- f[fx]=fy;
- ans+=e[i].w;
- }
- return ans ;
- }
- bool ok(int x,int y){
- return x>= && x<=n && y>= && y<=m && mp[x][y]!='#';
- }
- void bfs(int sx,int sy){
- queue <node> q;
- memset(d,INF,sizeof(d));d[sx][sy]=;
- node now;
- now.x=sx;now.y=sy;
- q.push(now);
- while(!q.empty()){
- node cur = q.front();q.pop();
- for(int i=;i<;i++){
- int x=cur.x+dx[i],y=cur.y+dy[i];
- if(!ok(x,y)||d[x][y]<=d[cur.x][cur.y]+) continue ;
- d[x][y]=d[cur.x][cur.y]+;
- now.x=x;now.y=y;
- q.push(now);
- if(mp[x][y]=='A'||mp[x][y]=='S'){
- e[++tot].u=num[sx][sy];e[tot].v=num[x][y];e[tot].w=d[x][y];
- }
- }
- }
- }
- int main(){
- cin>>t;
- while(t--){
- scanf("%d%d",&m,&n);
- tot = cnt = ;
- char c;
- while(){
- scanf("%c",&c);
- if(c!=' ') break ;
- }
- int first=;
- memset(num,,sizeof(num));
- for(int i=;i<=n;i++){
- getchar();
- first=;
- for(int j=;j<=m;j++){
- scanf("%c",&mp[i][j]);
- if(mp[i][j]=='S'||mp[i][j]=='A') num[i][j]=++cnt;
- }
- }
- for(int i=;i<=n;i++){
- for(int j=;j<=m;j++){
- if(num[i][j]) bfs(i,j);
- }
- }
- sort(e+,e+tot+);
- int ans = Kruskal();
- cout<<ans<<endl;
- }
- return ;
- }
POJ3026:Borg Maze (最小生成树)的更多相关文章
- POJ3026 Borg Maze(最小生成树)
题目链接. 题目大意: 任意两点(点表示字母)可以连线,求使所有点连通,且权值和最小. 分析: 第一感觉使3维的BFS.但写着写着,发现不对. 应当用最小生成树解法.把每个字母(即A,或S)看成一个结 ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- POJ3026 Borg Maze 2017-04-21 16:02 50人阅读 评论(0) 收藏
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14165 Accepted: 4619 Descri ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
- POJ3026 Borg Maze(bfs求边+最小生成树)
Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...
- (POJ 3026) Borg Maze 最小生成树+bfs
题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...
- poj 3026 Borg Maze (最小生成树+bfs)
有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...
随机推荐
- python 终极篇 --- django 视图系统
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- lintcode539 移动零
移动零 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数 您在真实的面试中是否遇到过这个题? Yes 样例 给出 ...
- Python3 数据类型-列表
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 索引如下图: 列表命名(list): 组成:使用[]括起来,并且 ...
- ZOJ 3644 Kitty's Game(数论+DP)
Description Kitty is a little cat. She is crazy about a game recently. There arenscenes in the game( ...
- Alpha冲刺——第三天
Alpha第三天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- TCP系列24—重传—14、F-RTO虚假重传探测
一.虚假重传 在一些情况下,TCP可能会在没有数据丢失的情况下初始化一个重传,这种重传就叫做虚假重传(Spurious retransmission).发生虚假重传的原因可能是包传输中重排序.传输中发 ...
- GPS定位,经纬度附近地点查询–C#实现方法
摘要:目前的工作是需要手机查找附近N米以内的商户,功能如下图数据库中记录了商家在百度标注的经纬度(如:116.412007,39.947545),最初想法以圆心点为中心点,对半径做 ...
- 3ds Max学习日记(一)
暑假闲来无事学习一发3ds Max.为啥要学这玩意?貌似可以用这东西三维建模.暑期生产实习选了一个搞vr的导师,貌似他忙得很,无奈只好先自己研究一下啦~ vr神马的还是有点意思的,虽然自己仅仅 ...
- Qt自定义标题栏
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt自定义标题栏 本文地址:http://techieliang.com/2017/1 ...
- Jenkins系列-Jenkins忘记密码的修复方法
找回 admin 用户的密码后,可以登录系统修改其他用户的密码. 1. Jenkins 目录结构 Jenkins 没有使用数据库,所有的信息都保存在 JENKINS_HOME 目录下的文件中.其中 J ...