結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 489 ms
42,280 KB
testcase_12 AC 433 ms
32,828 KB
testcase_13 AC 609 ms
44,652 KB
testcase_14 AC 233 ms
21,144 KB
testcase_15 AC 192 ms
17,600 KB
testcase_16 AC 624 ms
47,288 KB
testcase_17 AC 802 ms
55,564 KB
testcase_18 AC 803 ms
55,732 KB
testcase_19 AC 800 ms
53,572 KB
testcase_20 AC 427 ms
30,552 KB
testcase_21 AC 448 ms
31,536 KB
testcase_22 AC 178 ms
15,616 KB
testcase_23 AC 132 ms
12,928 KB
testcase_24 AC 368 ms
39,112 KB
testcase_25 AC 796 ms
54,876 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