前言
传送 :
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 水过去了
但是才发现 有自环和重边的情况
我没判断都过了
请问 最短路算法中 是否都可以跑 自环和重边
原因是什么
什么时候有需要判断自环和重边呢 ?