专题训练——最短路1 A到F题解
专题训练——最短路1 A到F题解
A. AtCoder-abc302_f Merge Set
核心思路
将数字和集合都看作图的节点:
- 数字节点:
1 \sim M - 集合节点:
M + 1 \sim M + N - 若数字
x 属于集合i ,则在x 和M + i 之间连无向边。
问题转化:从数字
路径经过
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, dist[N * 2];
vector<int> adj[N * 2];
queue<int> que;
void record(int x, int l) {
if (dist[x] != -1) {
return;
}
dist[x] = l;
que.push(x);
}
void bfs() {
record(1, 0);
while (!que.empty()) {
auto now = que.front();
que.pop();
for (auto i : adj[now]) {
record(i, dist[now] + 1);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
for (int j = 1; j <= a; j++) {
int s;
cin >> s;
adj[i + m].push_back(s);
adj[s].push_back(i + m);
}
}
for (int i = 1; i <= n + m; i++) {
dist[i] = -1;
}
bfs();
cout << dist[m] / 2 - 1;
return 0;
}
复杂度
时间复杂度:
空间复杂度:
B. CodeForces-1775D Friendly Spiders
核心思路
将每个数和它的质因子都看作图中的节点:
- 数节点:
1 \sim n - 质因子节点:
n + p (p 为质数)
若数
因此从
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int n, a[N], s, t, pre[N], dist[N], v;
vector<int> adj[N], p;
queue<int> q;
void record(int x, int l, int fa) {
if (dist[x] != -1) {
return;
}
q.push(x);
dist[x] = l;
pre[x] = fa;
}
void bfs() {
record(s, 0, 0);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto it : adj[u]) {
record(it, dist[u] + 1, u);
}
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
v = max(v, a[i]);
int x = a[i];
for (int j = 2; j * j <= x; j++) {
if (x % j == 0) {
adj[i].push_back(j + n);
adj[j + n].push_back(i);
for (; x % j == 0; x /= j){
}
}
}
if (x > 1) {
adj[x + n].push_back(i);
adj[i].push_back(x + n);
}
}
cin >> s >> t;
for(int i = 0; i <= n + v; i++){
dist[i] = -1;
}
bfs();
if (dist[t] == -1) {
cout << -1;
return 0;
}
cout << dist[t] / 2 + 1 << '\n';
for (int i = t; i != 0; i = pre[i]) {
p.push_back(i);
}
reverse(p.begin(), p.end());
for (auto it : p) {
if(it <= n){
cout << it << ' ';
}
}
return 0;
}
复杂度
时间复杂度:
C. CodeForces-1941G Rudolf and Subway
核心思路
将车站和颜色都视为图中的节点:
- 车站节点:编号
1 \sim n - 颜色节点:编号
n + c (c 为颜色编号)
对于每条边
这样,乘坐一条颜色为
问题转化:从起点车站
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int t, n, m, dist[N], b, e, c[N];
vector<int> adj[N];
queue<int> que;
void record(int x, int l) {
if (dist[x] != -1) {
return;
}
dist[x] = l;
que.push(x);
}
void bfs() {
record(b, 0);
while (!que.empty()) {
auto now = que.front();
que.pop();
for (auto i : adj[now]) {
record(i, dist[now] + 1);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v >> c[i];
adj[u].push_back(c[i] + n);
adj[v].push_back(c[i] + n);
adj[c[i] + n].push_back(u);
adj[c[i] + n].push_back(v);
dist[u] = dist[v] = dist[c[i] + n] = -1;
}
cin >> b >> e;
bfs();
cout << dist[e] / 2 << '\n';
for (int i = 1; i <= n; i++) {
adj[i].clear();
}
for (int i = 1; i <= m; i++) {
adj[c[i] + n].clear();
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> t;
while (t--) {
solve();
}
return 0;
}
复杂度
空间复杂度:
F. AtCoder-abc376_d Cycle
核心思路
要形成一个经过
目标:找到所有满足
- 先用
BFS 从1 出发,计算到所有顶点的最短距离dist 。 - 遍历所有边
(u, v) ,如果v == 1 且dist _ {u} 可达,则候选答案为dist _ {u} + 1 。 - 取最小值,若没有候选则输出
-1 。代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, INF = 1e9;
int n, m, dist[N]; vector<int> adj[N], s; queue<int> que;
void record(int x, int l) { if (dist[x] != INF) { return; } dist[x] = l; que.push(x); }
void bfs() { record(1, 0); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto i : adj[now]) { record(i, dist[now] + 1); } } }
int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); if (v == 1) { s.push_back(u); } } for (int i = 0; i <= n; i++) { dist[i] = INF; } bfs(); int ans = INF; for (int it : s) { if (dist[it] != -1) { ans = min(ans, dist[it]); } } cout << (ans == INF ? -1 : ans + 1); return 0; }
### 复杂度
- BFS:$ O(N + M) $
- 扫描边:$ O(M) $
- 总时间 $ O(N + M) $,空间 $ O(N + M) $。