結果

問題 No.807 umg tours
ユーザー kya_skikya_ski
提出日時 2019-10-26 18:53:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 713 ms / 4,000 ms
コード長 1,808 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 183,684 KB
実行使用メモリ 55,864 KB
最終ジャッジ日時 2024-05-03 00:03:52
合計ジャッジ時間 10,009 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 387 ms
42,280 KB
testcase_12 AC 397 ms
32,828 KB
testcase_13 AC 514 ms
44,520 KB
testcase_14 AC 201 ms
21,148 KB
testcase_15 AC 153 ms
17,604 KB
testcase_16 AC 557 ms
47,284 KB
testcase_17 AC 713 ms
55,564 KB
testcase_18 AC 706 ms
55,864 KB
testcase_19 AC 683 ms
53,568 KB
testcase_20 AC 370 ms
30,552 KB
testcase_21 AC 389 ms
31,540 KB
testcase_22 AC 166 ms
15,616 KB
testcase_23 AC 123 ms
12,928 KB
testcase_24 AC 359 ms
39,120 KB
testcase_25 AC 699 ms
54,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;
template<class T> inline bool chmax(T& a,T b)
{if (a < b) {a = b; return true;} return false;}
template<class T> inline bool chmin(T& a,T b)
{if (a > b) {a = b; return true;} return false;}
const long long INF = 1e12;

vector<long long> dijkstra(const Graph &g, int s) {
    vector<long long> dist(g.size(), INF);
    using Pi = pair<long long, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        long long cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v; long long nc; tie(v, nc) = e;
            if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main(){
    int N, M;
    cin >> N >> M;
    Graph G(N, vector<edge>());
    Graph H(2 * N, vector<edge>());
    for (int i = 0; i < M; i++) {
        int a, b;
        long long c;
        cin >> a >> b >> c;
        a--;
        b--;
        if(b != 0)G[a].push_back(make_pair(b, c));
        if(a != 0)G[b].push_back(make_pair(a, c));
        if(b != 0)H[a].push_back(make_pair(b, c));
        if(a != 0)H[b].push_back(make_pair(a, c));
        if(b != 0)H[N + a].push_back(make_pair(N + b, c));
        if(a != 0)H[N + b].push_back(make_pair(N + a, c));
        if(a != 0)H[b].push_back(make_pair(N + a, 0));
        if(b != 0)H[a].push_back(make_pair(N + b, 0));
    }
    
    vector<long long> time1 = dijkstra(G, 0);
    vector<long long> time2 = dijkstra(H, 0);
    for (int i = 0; i < N; i++) {
        if(i == 0)cout << 0 << endl;
        else cout << time1[i] + time2[i + N] << endl;
    }
    return 0;
}
    
0