結果

問題 No.807 umg tours
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-08-27 16:21:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 474 ms / 4,000 ms
コード長 1,828 bytes
コンパイル時間 6,189 ms
コンパイル使用メモリ 189,820 KB
実行使用メモリ 26,828 KB
最終ジャッジ日時 2024-05-02 23:58:23
合計ジャッジ時間 8,198 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 274 ms
16,432 KB
testcase_12 AC 272 ms
16,076 KB
testcase_13 AC 352 ms
22,348 KB
testcase_14 AC 165 ms
11,860 KB
testcase_15 AC 120 ms
9,748 KB
testcase_16 AC 381 ms
22,604 KB
testcase_17 AC 461 ms
25,076 KB
testcase_18 AC 454 ms
25,252 KB
testcase_19 AC 474 ms
26,828 KB
testcase_20 AC 287 ms
16,568 KB
testcase_21 AC 295 ms
17,056 KB
testcase_22 AC 119 ms
9,344 KB
testcase_23 AC 101 ms
7,936 KB
testcase_24 AC 275 ms
22,344 KB
testcase_25 AC 462 ms
26,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = MOD - 1;
const ll LLINF = 4e18;

// dijkstra法
struct edge { int to; ll cost; };
vector<vector<edge>> G;//グラフ
vector<vector<ll>> d;//sからの距離(INFで初期化)

void dijkstra(int s) {
    priority_queue<tuple<ll, int,int>, vector<tuple<ll, int, int>>, greater<tuple<ll, int, int>>> pq;
    d[s][0] = d[s][1] = 0;
    pq.push(tuple<ll, int, int>(0, 0, s));
    pq.push(tuple<ll, int, int>(0, 1, s));
    while (!pq.empty()) {
        tuple<int, int, int> p = pq.top(); pq.pop();
        int v = get<2>(p);
        if (d[v][get<1>(p)] < get<0>(p)) continue;
        for (edge e : G[v])
        {
            if (d[e.to][0] > d[v][0] + e.cost) {
                d[e.to][0] = d[v][0] + e.cost;
                pq.push({d[e.to][0],0, e.to});
            }
            if (d[e.to][1] > d[v][0]) {
                d[e.to][1] = min(d[v][0], d[v][1] + e.cost);
                pq.push({d[e.to][1],1, e.to});
            }
            if (d[e.to][1] > d[v][1] + e.cost) {
                d[e.to][1] = min(d[v][0],d[v][1] + e.cost);
                pq.push({d[e.to][1],1, e.to});
            }
        }
    }
}

int main() {
    int n, m; cin >> n >> m;
    G.resize(n);
    d.resize(n,vector<ll>(2,LLINF));
    REP(i, m) {
        int a, b;ll c; cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    dijkstra(0);
    REP(i, n) {
        cout << d[i][0] + d[i][1] << endl;
    }
    getchar(); getchar();
}
0