多源BFS
- 单源最短路问题vs多源最短路问题
- 当问题中只存在⼀个起点时,这时的最短路问题就是单源最短路问题。
- 当问题中存在多个起点⽽不是单⼀起点时,这时的最短路问题就是多源最短路问题
- 多源BFS
多源最短路问题的边权都为1 时,此时就可以⽤多源BFS来解决。 - 解决⽅式
把这些源点汇聚在⼀起,当成⼀个"超级源点"。然后从这个"超级源点"开始,处理最短路问题。落实到代码上时:- 初始化的时候,把所有的源点都加⼊到队列⾥⾯;
- 然后正常执⾏bfs的逻辑即可。
也就是初始化的时候,⽐普通的bfs多加⼊⼏个起点
矩阵距离
正难则反:
- 如果针对某⼀个点,直接去找最近的1,我们需要对所有的0都来⼀次bfs,这个时间复杂度是
接受不了的。 - 但是我们如果反着来想,从1开始向外扩展,每遍历到⼀个0就更新⼀下最短距离。这样仅需⼀次 bfs,就可以把所有点距离1的最短距离更新出来。
正难则反是很重要的思想,后续还有很多题可以⽤到这个思想。
由于1的数量很多,因此可以把所有的1看成⼀个超级源点,从这个超级源点开始⼀层⼀层的向外
扩展。实现起来也很简单,就是在初始化阶段把所有1的坐标加⼊到队列中,然后正常bfs。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m;
char a[N][N];
int dist[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void bfs()
{
memset(dist, -1, sizeof dist);
queue<PII> q;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j] == '1')
{
q.push({i, j});
dist[i][j] = 0;
}
}
}
//bfs
while (q.size())
{
auto t = q.front(); q.pop();
int x = t.first, y = t.second;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a >= 1 && a <= n && b >= 1 && b <= m && dist[a][b] == -1)
{
dist[a][b] = dist[x][y] + 1;
q.push({a, b});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
bfs();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << dist[i][j] << " ";
}
cout << endl;
}
return 0;
}
P1902 刺杀大使 - 洛谷
直接找答案显然是不现实的,因为能⾛的路径实在是太多了,如果全都枚举出来时间上吃不消。但是题⽬要求的是最⼤值最⼩化,可以尝试⽤⼆分来优化枚举。
设最终结果是x ,会发现⼀个性质:
- 当规定搜索过程中的最⼤值⼤于等于x 时,我们⼀定可以从第⼀⾏⾛到最后⼀⾏;
- 当规定搜索过程中的最⼤值⼩于x 时,我们⼀定不能⾛到最后⼀⾏。
因此,我们可以⼆分最终结果,通过bfs或者dfs来判断是否能⾛到最后⼀⾏。
如果⽤dfs,那就从第⼀⾏的每⼀列开始,全都搜索⼀遍。如果⽤bfs ,可以看成多源bfs问题,直接把所有的源点加⼊队列中,然后正常搜索即可
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m;
int p[N][N];
bool st[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
//伤害不超过mid的情况下,能够到达第n行
bool bfs(int mid)
{
if (n == 1) return 1;
memset(st, 0, sizeof st);
queue<PII> q;
//加入原点
for (int j = 1; j <= m; j++)
{
q.push({1, j});
st[1][j] = true;
}
while (q.size())
{
auto t = q.front(); q.pop();
int x = t.first, y = t.second;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a >= 1 && a <= n && b >= 1 && b <= m && p[a][b] <= mid && st[a][b] == false)
{
st[a][b] = true;
q.push({a, b});
if (a == n) return true;
}
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int l = 0, r = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> p[i][j];
r = max(r, p[i][j]);
}
}
//二分答案
while (l < r)
{
int mid = (l + r) / 2;
if (bfs(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
return 0;
}
01BFS
01BFS⼜称双端队列BFS。
在最短路问题中,边权值可能为1也可能为0。那么,在BFS的过程中,可以将边权为0扩展出来的点放到队⾸,边权为1扩展出来的点放到队尾。这样就能保证像普通BFS⼀样整个队列队⾸到队尾权值单调不下降。
注意:
不同于普通的bfs问题,01bfs问题在遍历的过程中,如果遇到之前已经遍历过的结点,有可能会找到⼀条更优的路线。在写代码的时候,要注意判断这⼀点
P4554 小明的游戏 - 洛谷
01bfs模板题。
- 如果⾛到相同格⼦,权值为0 ,更新最短距离,然后加⼊到队头;
- 如果⾛到不同格⼦,权值为1 ,更新最短距离,然后加⼊到队尾。
其余的搜索⽅式与常规bfs⼀模⼀样,但是注意松弛操作
#include <iostream>
#include <deque>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 510;
int n, m, x1, y1, x2, y2;
char a[N][N];
int dist[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void bfs()
{
if (x1 == x2 && y1 == y2)
{
dist[x2][y2] = 0;
return;
}
memset(dist, -1, sizeof dist);
deque<PII> q;
q.push_back({x1, y1});
dist[x1][y1] = 0;
while (q.size())
{
auto t = q.front(); q.pop_front();
int i = t.first, j = t.second;
if (i == x2 && j == y2) return;
for (int k = 0; k < 4; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 0 && x < n && y >= 0 && y < m)
{
char cur = a[i][j], next = a[x][y];
int w = (cur == next ? 0 : 1);
if (dist[x][y] == -1)
{
dist[x][y] = dist[i][j] + w;
if (w == 0) q.push_front({x, y});
else q.push_back({x, y});
}
else if (dist[i][j] + w < dist[x][y])
{
//松弛操作
dist[x][y] = dist[i][j] + w;
}
}
}
}
}
int main()
{
while (cin >> n >> m, n && m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
cin >> x1 >> y1 >> x2 >> y2;
bfs();
cout << dist[x2][y2] << endl;
}
return 0;
}
CF590C Three States - 洛谷
正难则反:
- 直接找出结果点是很⿇烦的,需要枚举所有的点,然后每个点都要来⼀次bfs,这样是会超时的。
- 可以依次从三个国家出发,⽤bfs计算出来到达所有点的最短距离。求出所有距离之后,重新遍历所有的点,分情况求出到三个国家的最短距离。
算法原理很简单,但是⾥⾯有很多细节需要注意:
- 因为每个国家可能有很多点,并且国家的点与点之间不连通,因此我们要⽤多源bfs求某个国家到所有点的最短距离;
- 国家与国家之间的点如果相连,它们之间的距离是0 (⽆论是不是同⼀个国家,在我们这道题⾥⾯,它们的距离都是0 ,因为都不需要修路)。那么我们这道题⾥⾯边的权值要么是1 ,要么是0。因此需要⽤01bfs来更新所有的距离;
- 计算某⼀个点到三个国家的最短距离时,应该分情况讨论。设a,b,c分别表⽰三个国家到该点的最短距离:
a. 如果该点是⼀个国家,最短距离为a + b + c ;
b. 如果该点是⼀个荒地,最短距离为a+b+c-2 ,因为我们计算最短距离的时候,荒地会被计算三次,所以要减去两次。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m;
char a[N][N];
int dist[4][N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void bfs(int num)
{
memset(dist[num], -1, sizeof dist[num]);
//加入原点
deque<PII> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (a[i][j] - '0' == num)
{
q.push_back({i, j});
dist[num][i][j] = 0;
}
}
//01bfs
while (q.size())
{
auto t = q.front(); q.pop_front();
int i = t.first, j = t.second;
for (int k = 0; k < 4; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#')
{
int w = (a[x][y] == '.' ? 1 : 0);
if (dist[num][x][y] == -1) //第一次遇到
{
dist[num][x][y] = dist[num][i][j] + w;
if (w) q.push_back({x, y});
else q.push_front({x, y});
}
else if (dist[num][i][j] + w < dist[num][x][y])
{
dist[num][x][y] = dist[num][i][j] + w;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
bfs(1); bfs(2); bfs(3);
int ret = 0x3f3f3f3f;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (a[i][j] == '#') continue;
int x = dist[1][i][j], y = dist[2][i][j], z = dist[3][i][j];
if (x == -1 || y == -1 || z == -1) continue;
if (a[i][j] == '.') ret = min(ret, x + y + z - 2);
else ret = min(ret, x + y + z);
}
if (ret == 0x3f3f3f3f) cout << -1 << endl;
else cout << ret << endl;
return 0;
}
Floodfill问题
Floodfill算法,⼜称漫⽔填充,像素法,本质是在寻找具有相同性质的联通块
P1596 [USACO10OCT] Lake Counting S - 洛谷
遍历整个矩阵,当遇到⼀个没有标记过的⽔坑时:
- 计数;
- 然后⽤bfs或者dfs将整个湖全都标记⼀下。
整个矩阵遍历⼀遍,就能得出湖的数量
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
char a[N][N];
bool st[N][N]; //标记搜索过的区域
int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
void dfs(int i, int j)
{
st[i][j] = true;
for (int k = 0; k < 8; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == 'W' && st[x][y] == false)
{
dfs(x, y);
}
}
}
void bfs(int i, int j)
{
queue<pair<int, int>> q;
q.push({i, j});
st[i][j] = true;
while (q.size())
{
auto t = q.front(); q.pop();
int i = t.first, j = t.second;
for (int k = 0; k < 8; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] == 'W' && st[x][y] == false)
{
st[x][y] = true;
q.push({x, y});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
int ret = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 'W' && st[i][j] == false)
{
ret++;
//dfs(i, j);
bfs(i, j);
}
}
cout << ret << endl;
return 0;
}
P1162 填涂颜色 - 洛谷
正难则反:直接找出被1包围的0是很困难的,因为很难确定当前搜索到的这个0是否是被包围的。但是,我们如果从边缘的0开始搜索,搜索到的0⼀定是没有被包围的。
因此,我们可以从边缘的0开始搜索,标记所有与边缘0相连的联通块。那么,没有被标记过的0就是被包围的。
⼩技巧:
- 我们可以把整个矩阵的外围包上⼀层 ,这样只⽤从
[0,0]
位置开始搜即可。不⽤遍历第⼀⾏第⼀列,最后⼀⾏以及最后⼀列
#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int n;
int a[N][N];
bool st[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int i, int j)
{
st[i][j] = true;
for (int k = 0; k < 4; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 0 && x <= n+1 && y >= 0 && y <= n+1 && a[x][y] == 0 && st[x][y] == 0)
{
dfs(x, y);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
dfs(0, 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j]) cout << a[i][j] << " ";
else if(st[i][j]) cout << 0 << " ";
else cout << 2 << " ";
}
cout << endl;
}
}
return 0;
}