专题训练——最短路1 A到F题解

· · 个人记录

专题训练——最短路1 A到F题解

A. AtCoder-abc302_f Merge Set

核心思路

将数字和集合都看作图的节点:

问题转化:从数字 1 走到数字 M 的最短路径长度 d (边数)。

路径经过 k 个集合节点,则 d = 2k ,合并这 k 个集合需要 k - 1 次操作,所以答案 = d / 2 - 1 。

代码

#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;
}

复杂度

时间复杂度: O(M + N + \sum a _ i)

空间复杂度: O(M + N + 2 \times \sum a _ i)

B. CodeForces-1775D Friendly Spiders

核心思路

将每个数和它的质因子都看作图中的节点:

若数 a _ i 含有质因子 p ,则在 i 和 n + p 之间连无向边。 这样,两个数若有公共质因子,则它们之间的路径长度为 2 。

因此从 s 到 t 的最短路径边数 d 必为偶数,答案 = d / 2 + 1 。 若不可达则输出 -1 。

代码

#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;
}

复杂度

时间复杂度: O(n · \sqrt A) ( A 为最大值)

C. CodeForces-1941G Rudolf and Subway

核心思路

将车站和颜色都视为图中的节点:

对于每条边 (u, v) 颜色为 c ,连接 u 与 (n + c) 和 v 与 (n + c) 。

这样,乘坐一条颜色为 c 的线路就相当于从车站走到颜色节点再走到另一车站。

问题转化:从起点车站 b 到终点车站 e 的最短路径边数 d ,答案 = d / 2 - 1 。 若不可达则输出 -1 。

代码

#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;
}

复杂度

## D. AtCoder-abc257_f Teleporter Setting ### 核心思路 将问题分为两类路径: 1. 不经过虚拟城市 $ 0 $ 的路径:直接在原图上行走。 预处理从 $ 1 $ 到所有点的最短距离 $ d1 $,从 $ N $ 到所有点的最短距离 $ dN $。 那么经过某个城市 $ u $ 的路径长度为 $ d1 _ {u} + dN _ {u} $。 若关闭城市 $ i $,只需选择 $ u \ne i $ 的最小值。 2. 经过虚拟城市 0 的路径:形如 $ 1 \to \dots \to a \to 0 \to b \to \dots \to N $。 长度为 $ d1 _ {a} + 1 + 1 + dN _ {b} = d1 _ {a} + dN _ {b} + 2 $。 最终答案为两种候选的最小值。 ### 代码 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, INF = 1e9; int n, m; int dist1[N], dist2[N]; vector<int> adj[N]; queue<int> que; void record1(int x, int l) { if (dist1[x] != -1) { return; } dist1[x] = l; que.push(x); } void record2(int x, int l) { if (dist2[x] != -1) { return; } dist2[x] = l; que.push(x); } void bfs1(int s){ while (!que.empty()) { que.pop(); } fill(dist1, dist1 + n + 1, -1); record1(s, 0); while (!que.empty()) { int u = que.front(); que.pop(); for (int v : adj[u]) { record1(v, dist1[u] + 1); } } } void bfs2(int s){ while (!que.empty()) { que.pop(); } fill(dist2, dist2 + n + 1, -1); record2(s, 0); while (!que.empty()) { int u = que.front(); que.pop(); for (int v : adj[u]) { record2(v, dist2[u] + 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); adj[v].push_back(u); } bfs1(1); bfs2(n); for(int i = 1; i <= n; i++){ int ans = INF; if (dist1[n] != -1) { ans = min(ans, dist1[n]); } if (dist1[i] != -1 && dist2[0] != -1) { ans = min(ans, dist1[i] + dist2[0]); } if (dist1[0] != -1 && dist2[i] != -1) { ans = min(ans, dist1[0] + dist2[i]); } if (ans != INF) { cout << ans << ' '; } else { cout << -1 << ' '; } } return 0; } ``` 时间复杂度:$ O(N + M) $。 ## E. AtCoder-abc244_f Shortest Good Path ### 核心思路 因为 $ N $ 很小,可以用状态压缩 $ BFS $ 求解。 - 状态:$ (k, u) $,表示当前已经访问过的顶点集合为 $ k $,且当前位于顶点 $ u $。 - 初始状态:对于每个顶点 $ i $,状态 $ (1 << i, i) $ 的距离为 $ 1 $。 - 转移:从当前状态 $ (k, u) $,走到邻接点 $ v $,新状态为 $ (k∣(1 << v), v) $,距离 $ + 1 $。 - $ BFS $ 保证每个状态第一次访问时的距离就是最短路径长度。 对所有非空 $ k $,取所有 $ u $ 的最小 $ dist _ {k, u} $,累加即为答案。 ### 代码 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 18, M = (1 << 18) + 1, INF = 1e9; int n, m; struct Node{ int s, u; }; vector<int> adj[N]; int dist[M][N]; queue<Node> que; void record(int s, int x, int l) { if (dist[s][x] != -1) { return; } dist[s][x] = l; que.push({s, x}); } void bfs(){ while (!que.empty()) { auto now = que.front(); que.pop(); int u = now.u, s = now.s; for (int v : adj[u]) { record(s ^ (1 << (v - 1)), v, dist[s][u] + 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); adj[v].push_back(u); } for (int i = 0; i < M; i++) { for(int j = 0; j < N; j++){ dist[i][j] = -1; } } for (int i = 1; i <= n; i++) { record(1 << (i - 1), i, 1); } bfs(); int ans = 0; for (int i = 0; i < (1 << n); i++) { if (i == 0) { continue; } int sum = INF; for (int j = 1; j <= n; j++) { if (dist[i][j] != -1) { sum = min(sum, dist[i][j]); } } ans += (sum == INF ? 0 : sum); } cout << ans; return 0; } ``` ### 复杂度 时间复杂度:$ O(N ^ 2 2 ^ N)

空间复杂度: O(2 ^ N · N)

F. AtCoder-abc376_d Cycle

核心思路

要形成一个经过 1 的环,只需满足:存在一条从 1 到某个顶点 u 的路径,且图中有一条边 u \to 1 。 那么环的长度 = dist[1 \to u] + 1 。

目标:找到所有满足 u \to 1 的顶点 u,取 dist _ {1, u} + 1 的最小值。

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) $。