直接拉的板子

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int N = 2e6 + 5;
int __t = 1, n;
vector<pair<int, int>> a[N];
vector<int> dist(N, INF);
void dijkstra(int s) {
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    dist[s] = 0;
    pq.push({0, s});
    while (!pq.empty()) {
        int d = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if (d > dist[u])
            continue;
        for (auto e : a[u]) {
            int v = e.first;
            int w = e.second;
            if (dist[u] + w < dist[v]) {
                dist[v] = dist[u] + w;
                pq.push({dist[v], v});
            }
        }
    }
}
void solve() {
    int n, m, s;
    cin >> n >> m >> s;
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        a[u].emplace_back(v, 1);
    }
    dijkstra(s);
    for (int i = 1; i <= n; ++i) {
        if (dist[i] == INF)
            cout << -1 << " ";
        else
            cout << dist[i] << " ";
    }
    cout << "\n";
    return;
}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    // cin >> __t;
    while (__t--)
        solve();
    return 0;
}