結果

問題 No.807 umg tours
ユーザー TiramisterTiramister
提出日時 2019-03-22 23:02:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 623 ms / 4,000 ms
コード長 2,041 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 90,368 KB
実行使用メモリ 46,140 KB
最終ジャッジ日時 2023-08-15 12:05:10
合計ジャッジ時間 8,562 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 345 ms
32,208 KB
testcase_12 AC 344 ms
24,264 KB
testcase_13 AC 468 ms
33,272 KB
testcase_14 AC 195 ms
16,080 KB
testcase_15 AC 150 ms
13,320 KB
testcase_16 AC 498 ms
35,176 KB
testcase_17 AC 609 ms
42,304 KB
testcase_18 AC 616 ms
42,016 KB
testcase_19 AC 603 ms
39,240 KB
testcase_20 AC 358 ms
22,520 KB
testcase_21 AC 376 ms
22,880 KB
testcase_22 AC 151 ms
11,936 KB
testcase_23 AC 113 ms
9,996 KB
testcase_24 AC 323 ms
35,168 KB
testcase_25 AC 623 ms
46,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
#include <limits>

using ll = long long;

template <class T>
struct Edge {
    int from, to;
    T cost;
    Edge(int from = -1, int to = -1, T cost = 1) : from(from), to(to), cost(cost){};

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

template <class T = int>
class Graph {
public:
    explicit Graph(int N = 0) : size(N) { path.resize(size); }
    void span(int u, int v, T cost = 1) { path[u].push_back(Edge<T>(u, v, cost)); }
    std::vector<Edge<T>> operator[](int v) const { return path[v]; }

    int size;
    std::vector<std::vector<Edge<T>>> path;
};

const ll INF = std::numeric_limits<ll>::max();

template <class T>
std::vector<T> dijkstra(const Graph<T>& graph, std::vector<int> ss) {
    std::vector<T> dist(graph.size, INF);
    std::priority_queue<std::pair<int, T>, std::vector<std::pair<int, T>>, std::greater<std::pair<int, T>>> que;

    for (auto s : ss) {
        dist[s] = 0;
        que.emplace(0, s);
    }

    while (!que.empty()) {
        int v;
        T d;
        std::tie(d, v) = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (const auto& e : graph[v]) {
            if (dist[e.to] <= dist[v] + e.cost) continue;
            dist[e.to] = dist[v] + e.cost;
            que.emplace(dist[e.to], e.to);
        }
    }
    return dist;
}

int main() {
    int N, M;
    std::cin >> N >> M;
    Graph<ll> graph(N * 2);
    for (int i = 0; i < M; ++i) {
        int u, v;
        ll c;
        std::cin >> u >> v >> c;
        --u, --v;
        graph.span(u, v, c);
        graph.span(v, u, c);
        graph.span(u, v + N, 0);
        graph.span(v, u + N, 0);
        graph.span(u + N, v + N, c);
        graph.span(v + N, u + N, c);
    }

    auto dist = dijkstra<ll>(graph, {0, N});
    for (int v = 0; v < N; ++v) {
        std::cout << dist[v] + dist[v + N] << std::endl;
    }
    return 0;
}
0