結果

問題 No.807 umg tours
ユーザー mencotton
提出日時 2020-11-02 21:47:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 543 ms / 4,000 ms
コード長 1,527 bytes
コンパイル時間 907 ms
コンパイル使用メモリ 80,916 KB
実行使用メモリ 42,156 KB
最終ジャッジ日時 2024-07-22 08:06:29
合計ジャッジ時間 7,429 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
using ll = long long;

struct edge {
    int to;
    ll cost;
};

struct status {
    ll cost;
    int v;

    bool operator<(const status &rhs) const { return cost < rhs.cost; };
    bool operator>(const status &rhs) const { return cost > rhs.cost; };
};

const ll INF = (1LL << 60) - 1;

vector<ll> dijkstra(int s, int n, vector<vector<edge>> &graph) {
    priority_queue<status, vector<status>, greater<>> que;
    vector<ll> dis(n, INF);
    dis[s] = 0;
    que.push({0, s});

    while (!que.empty()) {
        status now = que.top();
        que.pop();

        if (dis[now.v] < now.cost)continue;

        for (auto next:graph[now.v]) {
            if (dis[next.to] > dis[now.v] + next.cost) {
                dis[next.to] = dis[now.v] + next.cost;
                que.push({dis[next.to], next.to});
            }
        }
    }

    return dis;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<edge>> graph(n * 2);
    for (int i = 0; i < m; i++) {
        int u, v, cost;
        cin >> u >> v >> cost, u--, v--;
        graph[u].push_back({v, cost}), graph[v].push_back({u, cost});
        graph[u].push_back({v + n, 0}), graph[v].push_back({u + n, 0});
        graph[u + n].push_back({v + n, cost}), graph[v + n].push_back({u + n, cost});
    }

    vector<ll> dis = dijkstra(0, n * 2, graph);
    cout << 0 << endl;
    for (int i = 1; i < n; i++) cout << dis[i] + dis[i + n] << "\n";
    cout << flush;
    return 0;
}
0