poj3026(bfs+prim)最小生成树
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
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
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8
11
Source
代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int map[300][300],dis[300],vis[300];
char str[300][300];
int point[300][300];
int tvis[300][300],tdis[300][300];
struct node{
int x,y;
};
int m,n,ans;
int tnext[4][2]={1,0,0,1,-1,0,0,-1};
void bfs(int tx,int ty){
queue<node>q;
node next,res;
memset(tvis,0,sizeof(tvis));
memset(tdis,0,sizeof(tdis));
tvis[tx][ty]=1;
res.x=tx;
res.y=ty;
q.push(res);
while(!q.empty()){
res=q.front();
q.pop();
if(point[res.x][res.y]){
map[point[tx][ty]][point[res.x][res.y]]=tdis[res.x][res.y];
}
int xx,yy;
for(int k=0;k<4;k++){
next.x=xx=res.x+tnext[k][0];
next.y=yy=res.y+tnext[k][1];
if(xx>=1&&xx<=m&&yy>=1&&yy<=n&&!tvis[xx][yy]&&str[xx][yy]!='#'){
tvis[xx][yy]=1;
tdis[xx][yy]=tdis[res.x][res.y]+1;
q.push(next);
}
}
}
}
int prim(int u){
int sum=0;
for(int i=1;i<=ans;i++){
dis[i]=map[u][i];
}
vis[u]=1;
for(int ti=2;ti<=ans;ti++){
int tmin=2000000000;
int k;
for(int i=1;i<=ans;i++){
if(dis[i]<tmin&&!vis[i]){
tmin=dis[i];
k=i;
}
}
sum+=tmin;
vis[k]=1;
for(int j=1;j<=ans;j++){
if(dis[j]>map[k][j]&&!vis[j])
dis[j]=map[k][j];
}
}
return sum;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(point,0,sizeof(point));
memset(map,0,sizeof(map));
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(str,0,sizeof(str));
scanf("%d%d",&n,&m);
gets(str[0]);
ans=0;
for(int i=1;i<=m;i++){
gets(str[i]+1);
for(int j=1;j<=n;j++){
if(str[i][j]=='S'||str[i][j]=='A')
point[i][j]=++ans;
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(point[i][j])
bfs(i,j);
}
}
printf("%d\n",prim(1));
}
return 0;
}
poj3026(bfs+prim)最小生成树的更多相关文章
- 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)
1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...
- Prim 最小生成树算法
Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). P ...
- dijkstra(最短路)和Prim(最小生成树)下的堆优化
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- POJ3026(BFS + prim)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10554 Accepted: 3501 Descri ...
- POJ 3026(BFS+prim)
http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
随机推荐
- C# sqlhelp
public class SqlHelp { //数据库连接字符串 public static string connectionString = ConfigurationManager.Conne ...
- javascript 中数组的创建 添加 与将数组转换成字符串 页面三种提交请求的方式
创建js数组 var array=new Array(); Java中创建数组 private String[] array=new String[3]; 两个完全不同的,js中是可变长度的 添加内容 ...
- rest_framework --- viewsets
viewsets :from rest_framework import viewsets #导入方式 ViewSetMixin(object): 这个类,大致作用就是重写了as_view()方法,假 ...
- c++stl应用入门
在这篇中,我会讲几个简单易懂且比较常用的stl函数,这些函数在noip系列考试中往往被允许使用(既然让用我们自然不用手码了...) (末尾有惊喜!) 1.sort 绝大部分刚入门的oier第一个接触的 ...
- BZOj1261: [SCOI2006]zh_tree(dp)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 400 Solved: 272[Submit][Status][Discuss] Descriptio ...
- 【原创】展开二层嵌套列表(或pd.Series)的几种方法效率对比
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/10248763.html ★二层嵌套列表(或以列表为元素的pd.Series)有以下几种展开方式 (1)列 ...
- linux下进程的最大线程数、进程最大数、进程打开的文件数
linux下进程的最大线程数.进程最大数.进程打开的文件数 ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...
- idea中创建web项目搭建Hibernate框架连接oracle数据库
hibernate框架 hibernate是数据化持久工具,也是一个开源代码的ORM解决方案.hibernate内部封装了通过jdbc访问数据库的操作,向商场应用提供面向对象的数据访问api. hib ...
- 20180803UnionPay银联支付
LNMP环境下开发的银联支付(测试环境) 1.准备条件 a.银联支持API:https://open.unionpay.com/ajweb/help/api b.选择开发软件包 图示: c.我选择: ...
- 移植Linux Kernel SM750 驱动到VxWorks 7
一.SM750简介 SM750 是SiliconMotion 推出的一款适合嵌入式设备的显卡(Embedded GPU),采用PCIe接口与CPU连接,内部集成16MB DDR SDRAM显存,产品具 ...