POJ 3026(BFS+prim)
http://poj.org/problem?id=3026
题意:任意两个字母可以连线,求把所有字母串联起来和最小。
很明显这就是一个最小生成树,不过这个题有毒。他的输入有问题。在输入m和N后面,可能有一大串的空格。就因为这个,我RE都有点懵了,要不是discuss里面有人说输入有问题,我都没注意到这个,原本只用了一个getchar吃掉最后的换行符。没想到之后还有空格。有点小坑。
思路:这个题目如果他给你一个图,那就是最裸的prim了。不过这个题的难点也就是在他给的图你不能用Prim,你只能通过bfs去建立一个你可以新图,让这个新图去使用最小生成树。
#include <string.h>
#include <stdio.h>
#include <queue>
#define inf 0x3f3f3f
#define maxn 300 using namespace std; char mgraph[ maxn ][ maxn ];
int bfsgraph[ maxn ][ maxn ],dist[ maxn ][ maxn ],num[ maxn ][ maxn ],ans,dis[ maxn ];
bool mark[ maxn ][ maxn ],vis[ maxn ]; struct note{
int x,y;
}; void bfs(int x,int y) //我是把图建在bfsgraph里面的。
{
memset( dist , ,sizeof( dist ) );
memset( mark , true , sizeof( mark ) );
bfsgraph[ num[ x ][ y ] ][ num[ x ][ y ] ] = ;
queue<note >s;
note p,q;
p.x = x;
p.y = y;
dist[ x ][ y ] = ;
mark[ x ][ y ] = false;
s.push(p);
while(!s.empty())
{
p = s.front();
s.pop();
if(mark[ p.x + ][ p.y ] && mgraph[ p.x + ][ p.y ] != '#')
{
if( num[ p.x + ][ p.y ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x+ ][ p.y ]] = dist[ p.x ][ p.y ] + ; //如果当前点为字母的话,那么把这个点与起始点的距离给记录。
dist[ p.x + ][ p.y ] = dist[ p.x ][ p.y ] + ;
mark[ p.x + ][ p.y ] = false;
q.x = p.x + ;
q.y = p.y;
s.push(q);
}
if(mark[ p.x ][ p.y - ] && mgraph[ p.x ][ p.y - ] != '#')
{
if( num[ p.x ][ p.y - ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x ][ p.y - ]] = dist[ p.x ][ p.y ] + ;
dist[ p.x ][ p.y - ] = dist[ p.x ][ p.y ] + ;
mark[ p.x ][ p.y - ] = false;
q.x = p.x;
q.y = p.y - ;
s.push(q);
}
if(mark[ p.x ][ p.y + ] && mgraph[ p.x ][ p.y + ] != '#')
{
if( num[ p.x ][ p.y + ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x ][ p.y + ]] = dist[ p.x ][ p.y ] + ; dist[ p.x ][ p.y + ] = dist[ p.x ][ p.y ] + ;
mark[ p.x ][ p.y + ] = false;
q.x = p.x;
q.y = p.y + ;
s.push(q);
}
if(mark[ p.x - ][ p.y ] && mgraph[ p.x - ][ p.y ] != '#')
{
if( num[ p.x- ][ p.y ]) bfsgraph[ num[ x ][ y ] ][ num[ p.x - ][ p.y ]] = dist[ p.x ][ p.y ] + ;
dist[ p.x - ][ p.y ] = dist[ p.x ][ p.y ] + ;
mark[ p.x - ][ p.y ] = false;
q.x = p.x - ;
q.y = p.y;
s.push(q);
}
}
}
int prim(int pos)
{
for(int i = ; i < pos ; i++)
dis[ i ] = inf ; dis[ ] = ;
for(int i = ;i < pos ; i++){
int tep = inf;int k = ;
for(int j = ; j < pos ; j++){
if(vis[ j ]&&dis[ j ]<tep)
{
tep = dis[ j ];
k = j;
}
}
if(tep == inf) return ;
ans += tep;
vis[ k ]=false;
for(int j = ;j < pos ; j++)
if(vis[ j ]&&dis[ j ] > bfsgraph[ k ][ j ])
dis[ j ] = bfsgraph[ k ][ j ];
}
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int t,m,n;
char s[];
scanf("%d",&t);
while( t-- )
{
memset( mgraph , , sizeof( mgraph ) );
memset( bfsgraph , , sizeof( bfsgraph ) );
memset( num , , sizeof( num ) );
scanf("%d%d",&m,&n);
gets(s); //这里要注意,要吃掉m,n后面的空格,不然就等着RE吧。
ans = ;
int pos = ;
note point[ ];
for( int i = ; i < n ; i++ )
gets(mgraph[i]);
for( int i = ; i < n ; i++ )
for( int j = ; j < m ; j++ )
if( mgraph [ i ][ j ] == 'A' || mgraph[ i ][ j ] == 'S')
{
point[ pos ].x = i;
point[ pos ].y = j;
num[ i ][ j ] = pos; //我用num来编号,相当于一个映射,一个点集与一个数集之间的映射,因为要建图。
pos ++;
}
for( int i = ; i < pos ; i++ ) //对每一个点进行bfs。
bfs( point[ i ].x , point[ i ].y );
memset( vis , true , sizeof( vis ) );
prim(pos);
printf("%d\n",ans);
}
return ;
}
POJ 3026(BFS+prim)的更多相关文章
- J - Borg Maze - poj 3026(BFS+prim)
在一个迷宫里面需要把一些字母.也就是 ‘A’ 和 ‘B’连接起来,求出来最短的连接方式需要多长,也就是最小生成树,地图需要预处理一下,用BFS先求出来两点间的最短距离, *************** ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- poj3026(bfs+prim)最小生成树
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
随机推荐
- Python之路【第六篇】:面向对象编程相关
判断类与对象关系 isinstance(obj, cls) 判断对象obj是否是由cls类创建的 #!/usr/bin/env python #-*- coding:utf-8 -*- class ...
- 工具,百度编辑器 UEditor 使用实例化
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 为IIS Express添加MIME映射
VS2013自带IIS Express,无法发布JSON文件,需添加MIME映射. 没有图形界面,只能命令行. 进入C:\Program Files(x86)\IIS Express文件夹,输入:ap ...
- SQL笔记 - CTE递归实例(续):显示指定部门的全称
前一篇文章中已经可以取得所有部门的全称,但现在又有个新的需求: 只想得到某一个部门的部门全称,虽然可以用where条件来过滤,但是会有点小浪费. 这时我们可以从后往前找,先看下效果: 最后一条就是,行 ...
- Dirty Markup - 在线代码美化工具
如果你需要一个帮助你规整书写混乱的代码的工具的话,我强烈推荐给你这个在线代码美化工具 - Dirty Markup.这个在线工具能够帮助你有效的处理HTML/HTML5,CSS和javascript代 ...
- ProgressBar样式(转)
普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一般只要在XML布局中定义就可以了. <progressBar and ...
- 关于QString::toWCharArray 无法解析的外部符号
1>CommManger.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall QString::toWCharArray(un ...
- 【C语言入门教程】2.8 C 语言的预处理命令
预处理命令是在程序编译阶段进行执行的命令,用于编译与特定环境相关的可执行文件.预处理命令扩展了 C 语言,本节将选择其中一些常用的预处理命令进行讲解. 2.8.1 宏替换命令 宏替换命令的作用类似于对 ...
- Android手机如何通过USB共享网络给Mac?
最近网络挂了,mac不能上网查资料,心情非常毛躁,急切寻求用mac蹭WiFi的方法. 没有找到电脑端破解WiFi密码的软件,手头的Android手机没有root,也无法查看WiFi密码--->破 ...
- jQuery.extend()介绍
},{name:"Jerry",sex:"Boy"}) 得到的Result结果是: result={name:"Jerry",age:21, ...