結果

問題 No.160 最短経路のうち辞書順最小
ユーザー MisterMister
提出日時 2020-09-03 18:45:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 2,133 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 81,368 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 13:40:14
合計ジャッジ時間 3,006 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 10 ms
4,380 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 10 ms
4,376 KB
testcase_11 AC 10 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 9 ms
4,376 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 10 ms
4,376 KB
testcase_22 AC 10 ms
4,376 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 9 ms
4,376 KB
testcase_28 AC 14 ms
4,376 KB
testcase_29 AC 9 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

using lint = long long;

constexpr lint INF = 1LL << 30;

void solve() {
    int n, m, s, g;
    std::cin >> n >> m >> s >> g;

    Graph<int> graph(n);

    auto dist = vec(n, vec(n, INF));
    for (int v = 0; v < n; ++v) dist[v][v] = 0;

    while (m--) {
        int u, v, c;
        std::cin >> u >> v >> c;
        dist[u][v] = dist[v][u] = c;
        graph.span(false, u, v, c);
    }

    for (int j = 0; j < n; ++j) {
        for (int i = 0; i < n; ++i) {
            for (int k = 0; k < n; ++k) {
                dist[i][k] = std::min(dist[i][k],
                                      dist[i][j] + dist[j][k]);
            }
        }
    }

    while (s != g) {
        std::cout << s << " ";

        auto d = dist[s][g];
        int nxt = n;

        for (auto e : graph[s]) {
            int v = e.dst;
            if (e.cost + dist[v][g] == d) {
                nxt = std::min(nxt, v);
            }
        }
        s = nxt;
    }

    std::cout << g << "\n";
}

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

    solve();

    return 0;
}
0