sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

View the Project on GitHub sotanishy/cp-library-cpp

:heavy_check_mark: test/yosupo/shortest_path.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"

#include "../../graph/shortest_path.hpp"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, s, t;
    cin >> N >> M >> s >> t;
    vector<vector<pair<int, long long>>> G(N);
    for (int i = 0; i < M; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].push_back({b, c});
    }
    auto [dist, par] = shortest_path_tree(G, s);
    if (dist[t] >= 1e18) {
        cout << -1 << endl;
    } else {
        vector<int> path;
        for (int v = t; v != s; v = par[v]) {
            path.push_back(v);
        }
        path.push_back(s);
        reverse(path.begin(), path.end());
        cout << dist[t] << " " << path.size() - 1 << endl;
        for (int i = 0; i < (int)path.size() - 1; ++i) {
            cout << path[i] << " " << path[i + 1] << "\n";
        }
    }
}
#line 1 "test/yosupo/shortest_path.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"

#line 2 "graph/shortest_path.hpp"
#include <limits>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>

/*
 * Bellman-Ford Algorithm
 */
template <typename T>
std::vector<T> bellman_ford(const std::vector<std::tuple<int, int, T>>& G,
                            int V, int s) {
    constexpr T INF = std::numeric_limits<T>::max();
    std::vector<T> dist(V, INF);
    dist[s] = 0;
    for (int i = 0; i < V; ++i) {
        for (auto& [s, t, w] : G) {
            if (dist[s] != INF && dist[t] > dist[s] + w) {
                dist[t] = dist[s] + w;
                if (i == V - 1) return {};
            }
        }
    }
    return dist;
}

/*
 * Floyd-Warshall Algorithm
 */
template <typename T>
void floyd_warshall(std::vector<std::vector<T>>& dist) {
    const int V = dist.size();
    for (int k = 0; k < V; ++k) {
        for (int i = 0; i < V; ++i) {
            for (int j = 0; j < V; ++j) {
                dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

/*
 * Dijkstra's Algorithm
 */
template <typename T>
std::vector<T> dijkstra(const std::vector<std::vector<std::pair<int, T>>>& G,
                        int s) {
    std::vector<T> dist(G.size(), std::numeric_limits<T>::max());
    dist[s] = 0;
    using P = std::pair<T, int>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
    pq.emplace(0, s);

    while (!pq.empty()) {
        auto [d, v] = pq.top();
        pq.pop();
        if (dist[v] < d) continue;
        for (auto& [u, w] : G[v]) {
            if (dist[u] > d + w) {
                dist[u] = d + w;
                pq.emplace(dist[u], u);
            }
        }
    }

    return dist;
}

template <typename T>
std::pair<std::vector<T>, std::vector<int>> shortest_path_tree(
    const std::vector<std::vector<std::pair<int, T>>>& G, int s) {
    std::vector<T> dist(G.size(), std::numeric_limits<T>::max());
    std::vector<int> par(G.size(), -1);
    dist[s] = 0;
    using P = std::pair<T, int>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
    pq.emplace(0, s);

    while (!pq.empty()) {
        auto [d, v] = pq.top();
        pq.pop();
        if (dist[v] < d) continue;
        for (auto& [u, w] : G[v]) {
            if (dist[u] > d + w) {
                dist[u] = d + w;
                par[u] = v;
                pq.emplace(dist[u], u);
            }
        }
    }

    return {dist, par};
}

/*
 * Breadth-First Search
 */
std::vector<int> bfs(const std::vector<std::vector<int>>& G, int s) {
    std::vector<int> dist(G.size(), -1);
    dist[s] = 0;
    std::queue<int> que;
    que.push(s);

    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int u : G[v]) {
            if (dist[u] == -1) {
                dist[u] = dist[v] + 1;
                que.push(u);
            }
        }
    }

    return dist;
}

/*
 * Dial's Algorithm
 */
std::vector<int> dial(const std::vector<std::vector<std::pair<int, int>>>& G,
                      int s, int w) {
    std::vector<int> dist(G.size(), std::numeric_limits<int>::max());
    dist[s] = 0;
    std::vector<std::vector<int>> buckets(w * G.size(), std::vector<int>());
    buckets[0].push_back(s);

    for (int d = 0; d < (int)buckets.size(); ++d) {
        while (!buckets[d].empty()) {
            int v = buckets[d].back();
            buckets[d].pop_back();
            if (dist[v] < d) continue;
            for (auto& [u, w] : G[v]) {
                if (dist[u] > d + w) {
                    dist[u] = d + w;
                    buckets[dist[u]].push_back(u);
                }
            }
        }
    }
    return dist;
}
#line 4 "test/yosupo/shortest_path.test.cpp"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, s, t;
    cin >> N >> M >> s >> t;
    vector<vector<pair<int, long long>>> G(N);
    for (int i = 0; i < M; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].push_back({b, c});
    }
    auto [dist, par] = shortest_path_tree(G, s);
    if (dist[t] >= 1e18) {
        cout << -1 << endl;
    } else {
        vector<int> path;
        for (int v = t; v != s; v = par[v]) {
            path.push_back(v);
        }
        path.push_back(s);
        reverse(path.begin(), path.end());
        cout << dist[t] << " " << path.size() - 1 << endl;
        for (int i = 0; i < (int)path.size() - 1; ++i) {
            cout << path[i] << " " << path[i + 1] << "\n";
        }
    }
}
Back to top page