poj-3026—bfs+最小生成树
来源:互联网 发布:云计算发展历史 编辑:程序博客网 时间:2024/06/05 16:51
Description
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
Output
Sample Input
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
题意:这道题的意思是给你一个图,图中有点为'A'或者‘S’,求让这些点联通的最小生成树;(其实因为题意不想读,我是让队友给我说的题意,嘿嘿)
这道题我们要先用bfs将每个点取出来建好图,再跑一遍prim就可以了
尼玛。。。我用邻接表RE,矩阵就过了。。。。卧槽
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define nn 209
#define inff 0x3fffffff
struct node
{
int x,y,step;
};
int use[nn][nn];
char ma[nn][nn];
int hig,wid;
int id[nn][nn];
int dir[4][2]={ {1,0},
{-1,0},
{0,1},
{0,-1},
};
int sum_id;
/*struct point
{
int en,next,len;
}road[nn];
int cnt;
int p[nn];
void init()
{
cnt=0;
memset(p,-1,sizeof(p));
}
void add(int s,int e,int le)
{
road[cnt].en=e;
road[cnt].len=le;
road[cnt].next=p[s];
p[s]=cnt++;
}
bool vist[nn*10];
int dis[nn*10];
int prim()
{
int i,j;
// int dis[nn*10];
for(i=1;i<nn*10;i++)
dis[i]=inff;
dis[1]=0;
int res=0;
memset(vist,false,sizeof(vist));
int idd;
int w,k,h;
int mm;
for(i=1;i<sum_id;i++)
{
mm=inff;
for(j=1;j<sum_id;j++)
{
if(vist[j]==false&&dis[j]<mm)
{
idd=j;
mm=dis[j];
}
}
vist[idd]=true;
res+=dis[idd];
for(j=p[idd];j!=-1;j=road[j].next)
{
w=road[j].en;
dis[w]=min(dis[w],road[j].len);
}
}
return res;
}*/
int ss[nn][nn];
void init()
{
int i,j;
for(i=1;i<=nn;i++)
for(j=1;j<=nn;j++)
ss[i][j]=inff;
}
void add(int s,int e,int l)
{
ss[s][e]=l;
// ss[e][s]=l;
}
bool vis[nn];
int dis[nn];
void prim()
{
int i,j,mink,MM,ans=0;
for(i=1;i<=nn;i++)
vis[i]=false,dis[i]=inff;//和DIJ算法作用一样
dis[1]=0;
for(i=1;i<sum_id;i++)
{
for(j=1,MM=inff;j<sum_id;j++)
if(!vis[j]&&dis[j]<=MM)//选出权值最小且两个点分别在U和S集合里的边
{
MM=dis[j];
mink=j;
}
vis[mink]=true;//放进S集合
ans+=dis[mink];
for(j=1;j<=sum_id;j++)
if(!vis[j]&&ss[mink][j]<dis[j])//松弛U集合的点到S集合的距离
dis[j]=ss[mink][j];
}
printf("%d\n",ans);//输出最小生成树的权值和
}
void bfs(int sx,int sy)
{
queue<node> que;
int i,j;
int ty,tx;
memset(use,0,sizeof(use));
while(!que.empty())
que.pop();
node st,mid;
st.x=sx;
st.y=sy;
st.step=0;
que.push(st);
use[sx][sy]=1;
while(!que.empty())
{
mid=que.front();
que.pop();
if( ma[mid.x][mid.y]=='A'||ma[mid.x][mid.y]=='S')
{
add(id[sx][sy],id[mid.x][mid.y],mid.step);
add(id[mid.x][mid.y],id[sx][sy],mid.step);
}
for(i=0;i<4;i++)
{
tx=mid.x+dir[i][0];
ty=mid.y+dir[i][1];
if(tx>=0&& tx<hig && ty>=0 && ty<wid && ma[tx][ty]!='#' && use[tx][ty]!=1)
{
st.step=mid.step+1;
st.x=tx;
st.y=ty;
use[tx][ty]=1;
que.push(st);
}
}
}
}
int main()
{
int i,j;
int T;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d %d",&wid,&hig);
gets(ma[0]); //这里因为有空格 使用gets输入的,所以她会读入回车。所以我们要吃掉回车
for(i=0;i<hig;i++)
{
gets(ma[i]);
}
init();
int num=1;
for(i=0;i<hig;i++)
{
for(j=0;j<wid;j++)
{
if(ma[i][j]=='A'||ma[i][j]=='S')
{
id[i][j]=num;
num++;
}
}
}
sum_id=num;
for(i=0;i<hig;i++)
{
for(j=0;j<wid;j++)
{
if(ma[i][j]=='A'||ma[i][j]=='S')
{
bfs(i,j);
}
}
}
prim();
// printf("%d\n",prim());
// system("pause");
}
}
}
- 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建图+最小生成树
- poj 3026 Borg Maze (bfs+最小生成树)
- POJ 3026 Borg Maze【BFS+最小生成树】
- POJ 3026 Borg Maze (最小生成树 + BFS)
- POJ 3026 Borg Maze(BFS+最小生成树)
- POJ训练计划3026_Borg Maze(最小生成树+BFS)
- poj 3026 Borg Maze (bfs + 最小生成树)
- POJ 3026 Borg Maze (bfs+最小生成树)
- poj 3026 Borg Maze bfs+最小生成树
- POJ 3026 Brog Maze BFS+最小生成树
- hadoop process information unavailable
- linux常用命令--更改权限
- ShareSDK Android 微信分享 绕过审核与不绕过审核
- opencv 计时函数
- VisualCaptcha – 灵活的可视化验证码解决方案
- poj-3026—bfs+最小生成树
- CODE[VS] 1036 商务旅行(LCA + BFS)
- 物联网行业标准组织AllSeen Alliance成立---AllSeen AllJoyn 一回事了
- 2015年大一下第6周项目4-String类的构造
- webView loadData 中文乱码问题
- 对RFID安全机制的分析和解决方案
- 【SPOJ】Triple Sums【FFT】
- ios笔记-NSDictionary
- 第八周项目四 【炒鸡难!!高能预警!】String类的构造(重载双目运算符)