本文主要内容:
- 基础复习
- 利用bfs进行拓扑排序
- 多源bfs
1 bfs基础
- 主要功能:
- 求边权相等的图的单源最短路
- 层序遍历
- 注意事项:
- 队列元素类型的确定
- 判断状态数组更新的位置是在弹出队头时还是在将新元素加入队列时(取决于队列是否允许存在重复元素)
模板题1:Acwing844. 走迷宫
#include<iostream>
#include<queue>
using namespace std;
const int N = 105;
typedef pair<int, int> PII;
int g[N][N], n, m;
int dist[N][N];
int bfs()
{
queue<PII>q;
q.push({1, 1});
dist[1][1] = 0;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
while(q.size())
{
PII t = q.front();
q.pop();
int a = t.first, b = t.second;
if(a == n && b == m)return dist[n][m];
for(int i = 0; i < 4; i++)
{
int x = a + dx[i], y = b + dy[i];
if(!x || x > n || !y || y > m || g[x][y] == 1 || dist[x][y])continue;
dist[x][y] = dist[a][b] + 1;
q.push({x, y});
}
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> g[i][j];
cout << bfs();
return 0;
}
模板题2:Acwing845. 八数码
#include<iostream>
#include<unordered_map>
#include<queue>
using namespace std;
string st;
int bfs()
{
unordered_map<string, int>dist;
queue<string>q;
string ed = "12345678x";
q.push(st);
dist[st] = 0;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
while(q.size())
{
string s = q.front();
q.pop();
if(s == ed)return dist[s];
int distance = dist[s];
int k = s.find('x');
int a = k / 3, b = k % 3;
for(int i = 0; i < 4; i++)
{
int x = a + dx[i], y = b + dy[i];
if(x < 0 || x >= 3 || y < 0 || y >= 3)continue;
swap(s[k], s[x * 3 + y]);
if(!dist[s])
{
dist[s] = distance + 1;
q.push(s);
}
swap(s[k], s[x * 3 + y]);
}
}
return -1;
}
int main()
{
for(int i = 0; i < 9; i++)
{
char c;
cin >> c;
st += c;
}
cout << bfs();
return 0;
}
AcWing 847. 图中点的层次
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 100005;
int idx, e[N], ne[N], h[N];
int n, m;
int dist[N];
queue<int> q;
bool st[N];
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
int bfs()
{
q.push(1);
dist[1] = 0;
while(q.size())
{
auto t = q.front();
q.pop();
int distance = dist[t];
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(!st[j])
{
dist[j] = distance + 1;
q.push(j);
st[j] = true;
}
}
}
return dist[n];
}
int main()
{
memset(h, -1, sizeof h);
memset(dist, -1, sizeof dist);
cin >> n >> m;
while(m--)
{
int a, b;
cin >> a >> b;
add(a, b);
}
cout << bfs() << endl;
return 0;
}
2 利用bfs进行拓扑排序
例题:Acwing848. 有向图的拓扑序列
- 思路:
- 用一个数组cnt[]存储所有节点的入度,在输入边时边的右端点入度加1
- 遍历所有节点,将入度为0的节点放入队列
- 每次弹出一个节点存入,遍历该节点所指向的所有相邻节点,所以相邻节点的入度减1
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 100005;
int idx, e[N], ne[N], h[N];
int n, m;
int cnt[N], res[N], c;
queue<int>q;
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool bfs()
{
for(int i = 1; i <= n; i++)
if(!cnt[i])q.push(i);
while(q.size())
{
int t = q.front();
q.pop();
res[c++] = t;
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
cnt[j] -= 1;
if(!cnt[j])q.push(j);
}
}
return c == n;
}
int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
while(m--)
{
int a, b;
cin >> a >> b;
add(a, b);
cnt[b] ++;
}
if(bfs())
{
for(int i = 0; i < c; i++)
cout << res[i] << ' ';
}
else cout << "-1" << endl;;
return 0;
}
3 多源bfs
例题:Acwing173. 矩阵距离
- 思路:
- 最短路的起点有多个
- 将所有起点放入队列,然后bfs
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int dist[N][N], n, m;
char g[N][N];
void bfs()
{
memset(dist, 0x3f, sizeof dist);
queue<PII>q;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(g[i][j] == '1')q.push({i, j});
while(q.size())
{
PII t = q.front();
q.pop();
int x = t.first, y = t.second;
//cout << x << ' ' << y << endl;
if(g[x][y] == '1')dist[x][y] = 0;
for(int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a > n || b < 0 || b > n)continue;
if(g[a][b] == '1' || dist[a][b] <= dist[x][y] + 1)continue;
dist[a][b] = dist[x][y] + 1;
q.push({a, b});
}
}
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++)cin >> g[i];
bfs();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
cout << dist[i][j] << ' ';
cout << endl;
}
return 0;
}