带权值的图 BFS
用bfs遍历最图求最短路径时通常借用优先队列即优先考虑最大的或者最小的权值
方法1 优先队列:(内置函数,优先考虑较小的权值)
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct node{
int x,y,c;
bool friend operator < (node a,node b){
return a.c > b.c;//小的优先出来哦(没错就是大于号)
}
}r,w;
int n;
int dis[+][+];
int vis[+][+];
int ma[+][+];
int d[][]={,,,,-,,,-};
void bfs(int xx,int yy){ //bfs求终点到其余各点的最短路
priority_queue<node> q;
r.x = r.y = xx;
r.c = ma[n-][n-]; //以终点作为起点
dis[n-][n-] = ma[n-][n-];
vis[n-][n-] = ;
q.push(r);
while(!q.empty()){
r = q.top();
q.pop();
for(int i = ; i < ; i++){
int nx = r.x + d[i][];
int ny = r.y + d[i][];
if(nx < || ny < || nx >= n || ny >= n || vis[nx][ny]) continue;
w.x = nx;
w.y = ny;
w.c = r.c + ma[nx][ny];//把点(nx,ny)处的权值加上
vis[nx][ny] = ;//标记
q.push(w);
dis[nx][ny]=w.c;//跟新数组,保证每次都是最优的
}
} }
int main(){
cin>>n;
for(int i=;i< n;i++){
for(int j=;j< n;j++){
cin>>ma[i][j];
}
}
bfs(n-,n-);//这样一来数组里保存的就是其他点到点(n-1,n-1)的距离了;
for(int i=;i<n;i++)
{
cout<<endl;
for(int j=;j<n;j++)
printf("%d ",dis[i][j]);
}
return ; }
方法2:队列加判断条件(开一个数组step,加一个判断条件step [ d x ] [ d y ] > step [x] [y] + mp [dx] [dy] )
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define INF 100000000
using namespace std;
typedef long long ll;
struct stu{
int a,b;
};
int n;
ll arr[][];
ll step[][];
ll dp[][];
int d[][]={,,,,,-,-,};
void bfs(int x,int y){
queue<stu>que;
que.push({x,y});
step[x][y]=arr[x][y];
while(que.size()){
int xx=que.front().a;
int yy=que.front().b;
que.pop();
for(int i=;i<;i++){
int dx=xx+d[i][];
int dy=yy+d[i][];
if(dx>=&&dy>=&&dx<=n&&dy<=n){
if(step[dx][dy]>step[xx][yy]+arr[dx][dy]){
step[dx][dy]=step[xx][yy]+arr[dx][dy];//更新step数组,使他保存较小的的距离
que.push({dx,dy});
}
}
}
}
} int main(){
while(cin>>n)
{
for(int i=;i<=;i++){
for(int j=;j<=;j++)
step[i][j]=INF;
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>arr[i][j];
}
}
bfs(n,n);
for(int i=;i<=n;i++){
cout<<endl;
for(int j=;j<=n;j++)
cout<<step[i][j];//数组里的每一个点都是到(n,n)的最短距离
}
}
return ;
}
数据:
3
1 2 3
1 2 3
1 2 3
带权值的图 BFS的更多相关文章
- 51nod1459(带权值的dijkstra)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...
- HDU 1863:畅通project(带权值的并查集)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 洛谷 P2024 [NOI2001]食物链——带权值的并查集维护
先上一波题目 https://www.luogu.org/problem/P2024 通过这道题复习了一波并查集,学习了一波带权值操作 首先我们观察到 所有的环都是以A->B->C-> ...
- 带权值的LCA
例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...
- nyoj-----284坦克大战(带权值的图搜索)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" ...
- 【NOIP 2015 D1 T2】信息传递(图论--带权并查集/bfs)
题目:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- P - 奔小康赚大钱 - hdu 2255(带权值的匹配)
分析:这是一个KM的模板题,也就不多说了,KM最复杂的情况都能过,下面是没有优化过的代码: ****************************************************** ...
- 无向带权图的最小生成树算法——Prim及Kruskal算法思路
边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...
随机推荐
- Apache服务器故障排除攻略
Apache服务器故障排除攻略 应用服务器Apache浏览器配置管理网络应用 随着网络技术的普及.应用和Web技术的不断完善,Web服务已经成为互联网上重要的服务形式之一.原有的客户端/服务器模式正 ...
- python之面向对象性封装,多态,以及鸭子类型
默认类型 class A: class_name = 'python23期' def __init__(self, name, age): self.name = name self.age =age ...
- java 环境变量配置与第一个程序运行
从开始下载jdk,到运行出java第一个程序 ,花了5天时间 ,不过我相信万事开头难 ,以后会越来越好的 ,加油! jdk的下载: 在oracle官网上即可下载,jdk安装包,下载完以后运行安装 ,路 ...
- 干净直接安装+PS下载
PS CS6 https://www.cr173.com/soft/247727.html 直接一键安装,很方便干净. 不要去华军软件下,广告浪费时间. 链接:https://pan.baidu.co ...
- 12c OCR corrupted results in CRS stack down.
12c OCR corrupted results in CRS stack down. 1. check crsd.trc2017-03-21 16:14:44.667838 : CRSOCR:2 ...
- LVS的部署、案例、以及常见问题
LVS的部署.案例.以及常见问题 原创chenhuyang 最后发布于2018-06-03 16:18:25 阅读数 1560 收藏 展开 一.LVS的部署 LVS现在已经集成在linux内核模块中, ...
- Spring装配Bean的三种方式+导入和混合配置
目录 Spring IoC与bean 基于XML的显式装配 xml配置的基本结构 bean实例的三种创建方式 依赖注入的两种方式 构造器注入方式 setter方法注入方式 利用命名空间简化xml 基于 ...
- B. Lost Number【CF交互题 暴力】
B. Lost Number[CF交互题 暴力] This is an interactive problem. Remember to flush your output while communi ...
- G1垃圾回收器
垃圾回收器的发展历程 背景 01.G1解决的问题 G1垃圾回收器是04年正式提出,12开始正式支持,在17年作为JDK9默认的垃圾处理器. 在04年的时候,java程序堆的内存越来越大,从而导致程序中 ...
- 1051 Pop Sequence (25分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...