POJ 3026(BFS+最小生成树)
来源:互联网 发布:js移除元素属性 编辑:程序博客网 时间:2024/06/06 09:23
Borg Maze
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12098 Accepted: 3958
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.
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
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
题目大意:
一个迷宫中有一些点,挑选最短的路线能使每个点相连。
解题思路:
很典型的bfs+最小生成树,先用bfs求出起始点到每个点的最短距离,然后用最小生成树求出最短总距离。
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<bitset>#include<queue>#include<stack>#include<list>#include<set>#include<math.h>#include<map>using namespace std;void exchange(int &a,int &b){int c=a;a=b;b=c;}int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int n,m;char c[55][55];int a[55][55];int d[105][105];int cnt;int jud(int i,int j){if(a[i][j]==-1||i<0||i>=n||j<0||j>=m)return false;return true;}struct point{int x,y,step;point(int x,int y,int step):x(x),y(y),step(step){}point(){}};void bfs(int x,int y){int vis[55][55];memset(vis,0,sizeof(vis));vis[x][y]=1;queue<point>q;q.push(point(x,y,0));while(!q.empty()){point t=q.front();q.pop();if(a[t.x][t.y]>0){d[a[x][y]][a[t.x][t.y]]=t.step;}for(int i=0;i<4;i++)if(jud(t.x+dir[i][0],t.y+dir[i][1])&&!vis[t.x+dir[i][0]][t.y+dir[i][1]]){q.push(point(t.x+dir[i][0],t.y+dir[i][1],t.step+1));vis[t.x+dir[i][0]][t.y+dir[i][1]]=1;}}}int prim(){int s=1;int mm=1;int sum=0;int pos;int vis[105];memset(vis,0,sizeof(vis));vis[1]=1;int lowdis[5005];for(int i=0;i<cnt;i++)lowdis[i]=99999;while(true){if(mm==cnt-1)break;int mind=99999;for(int i=1;i<cnt;i++){if(!vis[i]&&d[s][i]&&lowdis[i]>d[s][i]){lowdis[i]=d[s][i];}if(!vis[i]&&mind>lowdis[i]){mind=lowdis[i];pos=i;}}s=pos;vis[s]=1;sum+=mind;mm++;}return sum;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int t;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);memset(c,0,sizeof(c));char c1[105];gets(c1);for(int i=0;i<n;i++){gets(c[i]);}cnt=1;memset(a,0,sizeof(a));for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(c[i][j]=='#')a[i][j]=-1;if(c[i][j]=='A'||c[i][j]=='S'){a[i][j]=cnt;cnt++;}}memset(d,0,sizeof(d));for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(a[i][j]>0)bfs(i,j);printf("%d\n",prim());}return 0;}
0 0
- poj 3026(BFS+最小生成树)
- POJ 3026(BFS+最小生成树)
- POJ 3026 bfs+最小生成树
- poj-3026—bfs+最小生成树
- POJ 3026 (BFS+最小生成树)
- POJ 3026 Borg Maze (最小生成树 + BFS)
- POJ 3026 Borg Maze(BFS+最小生成树)
- poj 3026 Borg Maze(BFS+最小生成树)
- POJ 3026 Borg Maze(bfs+最小生成树-Prim)
- POJ 3026 Borg Maze(最小生成树--prime+BFS)
- POJ 3026 Borg Maze(BFS+最小生成树)
- POJ-3026 Borg Maze (BFS+最小生成树)
- POJ 3026 Borg Maze(bfs+最小生成树)
- POJ 3026-Borg Maze(BFS+prim最小生成树)
- poj 3026 Borg Maze 最小生成树+bfs
- POJ 3026 Borg Maze bfs + 最小生成树
- poj 3026 Borg Maze bfs建图+最小生成树
- poj 3026 Borg Maze (bfs+最小生成树)
- MySQL存储过程详解
- 解决 Qt5 使用 OpenGL 函数未定义的错误
- HDU 1022 Train Problem I
- 11.1使用关联容器
- EL表达式简介
- POJ 3026(BFS+最小生成树)
- 安卓自定义MediaController的UI界面
- android中的坐标偏移分析与问题解决
- leetcode-Palindrome Number-9
- C#当窗体大小改变时,窗体中的控件大小也随之改变
- HDU 1021 Fibonacci Again
- 数据结构2.带控制信息的链表
- 学习Git(对廖雪峰Git教程的摘抄笔记)
- vim批量替换