Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/graph/shortest_path.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/graph/dijkstra.hpp"
int main(void) {
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    Graph<ll> g(n);
    rep(i, 0, m) {
        int a, b, c;
        cin >> a >> b >> c;
        g.add_directed_edge(a, b, c);
    }
    vector<pair<ll, int>> d = dijkstra(g, s);
    if(d[t].first == numeric_limits<ll>::max()) {
        cout << -1 << '\n';
        return 0;
    }
    vector<int> path;
    int cur = t;
    path.push_back(cur);
    while(d[cur].second != -1) {
        cur = d[cur].second;
        path.push_back(cur);
    }
    reverse(path.begin(), path.end());
    cout << d[t].first << ' ' << (int)path.size() - 1 << '\n';
    rep(i, 0, (int)path.size() - 1) {
        cout << path[i] << ' ' << path[i + 1] << '\n';
    }
}
#line 1 "verify/library_checker/graph/shortest_path.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/graph/dijkstra.hpp"
template <typename T>
vector<pair<T, int>> dijkstra(const Graph<T>& g, const int s = 0) {
    const int n = g.size();
    assert(0 <= s and s < n);
    vector<pair<T, int>> d(n, {numeric_limits<T>::max(), -1});
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
    d[s] = {0, -1};
    pq.emplace(0, s);
    while(!pq.empty()) {
        const auto [dist, cur] = pq.top();
        pq.pop();
        if(d[cur].first < dist) continue;
        for(const Edge<T>& e : g[cur]) {
            if(d[e.to].first > d[cur].first + e.cost) {
                d[e.to] = {d[cur].first + e.cost, cur};
                pq.emplace(d[e.to].first, e.to);
            }
        }
    }
    return d;
}
#line 5 "verify/library_checker/graph/shortest_path.test.cpp"
int main(void) {
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    Graph<ll> g(n);
    rep(i, 0, m) {
        int a, b, c;
        cin >> a >> b >> c;
        g.add_directed_edge(a, b, c);
    }
    vector<pair<ll, int>> d = dijkstra(g, s);
    if(d[t].first == numeric_limits<ll>::max()) {
        cout << -1 << '\n';
        return 0;
    }
    vector<int> path;
    int cur = t;
    path.push_back(cur);
    while(d[cur].second != -1) {
        cur = d[cur].second;
        path.push_back(cur);
    }
    reverse(path.begin(), path.end());
    cout << d[t].first << ' ' << (int)path.size() - 1 << '\n';
    rep(i, 0, (int)path.size() - 1) {
        cout << path[i] << ' ' << path[i + 1] << '\n';
    }
}
Back to top page