您当前的位置: 首页 > 

*DDL_GzmBlog

暂无认证

  • 2浏览

    0关注

    605博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

[Acwing] 847. 图中点的层次 bfs || spfa

*DDL_GzmBlog 发布时间:2021-10-31 22:37:55 ,浏览量:2

前言

传送 :

bfs方法待补

前言

无非就是算一遍 从1开始的最短路

CODE
 // Problem: 图中点的层次
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/849/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include 
using namespace std;
#define ll long long
#define endl '\n'
#define px first
#define py second

typedef pair pii;
int dxy[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
const int N  = 1e5+10;

int dist[N];
int n,m,t;
vector g[N];
int st[N];
typedef pair pii;
map mp;


void spfa()
{
	queue q;
	memset(dist,0x3f,sizeof dist);
	
	dist[1] = 0;
	st[1] =1;
	q.push(1);
	
	while(!q.empty())
	{
		int t = q.front();
		q.pop();
		st[t] =0 ;
		
		for(auto x : g[t])
		{
			if(dist[x] > dist[t] + 1)
			{
				dist[x] = dist[t] +1;
				if(!st[x])
				{
					q.push(x);
					st[x] = 1;
				}
			}
		}
	}
	
	if(dist[n]!=0x3f3f3f3f)
	coutb;
		g[a].push_back(b);
	}
	
	spfa();
	
}

int main()
{
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
疑虑

847. 图的层次

刚刚在这题中 使用了spfa 水过去了

但是才发现 有自环和重边的情况

我没判断都过了

请问 最短路算法中 是否都可以跑 自环和重边

原因是什么

什么时候有需要判断自环和重边呢 ?

关注
打赏
1657615554
查看更多评论
立即登录/注册

微信扫码登录

0.0361s