1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
// 基本 BFS
void bfs(int start, const vector<vector<int>>& adj)
{
int n = adj.size();
vector<bool> visited(n, false);
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
cout << u << " ";
for (int v : adj[u])
{
if (!visited[v])
{
visited[v] = true;
q.push(v);
}
}
}
cout << endl;
}
// BFS 求最短路径
vector<int> shortest_path(int start, int end, const vector<vector<int>>& adj)
{
int n = adj.size();
vector<int> dist(n, -1);
vector<int> parent(n, -1);
queue<int> q;
q.push(start);
dist[start] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == end)
{
break;
}
for (int v : adj[u])
{
if (dist[v] == -1)
{
dist[v] = dist[u] + 1;
parent[v] = u;
q.push(v);
}
}
}
vector<int> path;
if (dist[end] == -1)
{
return path;
}
for (int v = end; v != -1; v = parent[v])
{
path.push_back(v);
}
reverse(path.begin(), path.end());
return path;
}
// 多源 BFS
vector<vector<int>> multi_source_bfs(int n, int m, const vector<pair<int, int>>& sources)
{
vector<vector<int>> dist(n, vector<int>(m, -1));
queue<pair<int, int>> q;
for (const auto& source : sources)
{
int x = source.first;
int y = source.second;
dist[x][y] = 0;
q.emplace(x, y);
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
while (!q.empty())
{
pair<int, int> front = q.front();
int x = front.first;
int y = front.second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && dist[nx][ny] == -1)
{
dist[nx][ny] = dist[x][y] + 1;
q.emplace(nx, ny);
}
}
}
return dist;
}
// 0-1 BFS
vector<int> zero_one_bfs(int start, const vector<vector<pair<int, int>>>& adj)
{
int n = adj.size();
vector<int> dist(n, INT_MAX);
deque<int> dq;
dist[start] = 0;
dq.push_front(start);
while (!dq.empty())
{
int u = dq.front();
dq.pop_front();
for (const auto& edge : adj[u])
{
int v = edge.first;
int w = edge.second;
if (dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
if (w == 0)
{
dq.push_front(v);
}
else
{
dq.push_back(v);
}
}
}
}
return dist;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// 测试基本 BFS
int n = 6;
vector<vector<int>> adj(n);
adj[0] = {1, 2};
adj[1] = {0, 3, 4};
adj[2] = {0, 5};
adj[3] = {1};
adj[4] = {1};
adj[5] = {2};
cout << "BFS 遍历: ";
bfs(0, adj);
// 测试最短路径
vector<int> path = shortest_path(0, 5, adj);
cout << "最短路径: ";
for (int v : path)
{
cout << v << " ";
}
cout << endl;
// 测试多源 BFS
int rows = 3, cols = 3;
vector<pair<int, int>> sources = {{0, 0}, {2, 2}};
vector<vector<int>> dist = multi_source_bfs(rows, cols, sources);
cout << "多源 BFS 距离矩阵: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << dist[i][j] << " ";
}
cout << endl;
}
return 0;
}
|