結果

問題 No.807 umg tours
ユーザー tactac
提出日時 2019-10-26 17:44:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,264 ms / 4,000 ms
コード長 3,125 bytes
コンパイル時間 2,190 ms
コンパイル使用メモリ 187,816 KB
実行使用メモリ 100,884 KB
最終ジャッジ日時 2024-05-03 00:01:55
合計ジャッジ時間 15,152 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
7,992 KB
testcase_01 AC 7 ms
7,892 KB
testcase_02 AC 8 ms
8,020 KB
testcase_03 AC 7 ms
8,016 KB
testcase_04 AC 7 ms
7,984 KB
testcase_05 AC 7 ms
7,988 KB
testcase_06 AC 8 ms
7,968 KB
testcase_07 AC 7 ms
8,008 KB
testcase_08 AC 8 ms
7,828 KB
testcase_09 AC 8 ms
7,836 KB
testcase_10 AC 7 ms
7,968 KB
testcase_11 AC 530 ms
32,136 KB
testcase_12 AC 770 ms
38,680 KB
testcase_13 AC 1,415 ms
61,776 KB
testcase_14 AC 462 ms
33,440 KB
testcase_15 AC 301 ms
32,896 KB
testcase_16 AC 662 ms
44,016 KB
testcase_17 AC 1,303 ms
46,296 KB
testcase_18 AC 2,264 ms
100,884 KB
testcase_19 AC 1,695 ms
64,652 KB
testcase_20 AC 314 ms
19,888 KB
testcase_21 AC 322 ms
20,408 KB
testcase_22 AC 125 ms
13,184 KB
testcase_23 AC 99 ms
12,292 KB
testcase_24 AC 261 ms
32,572 KB
testcase_25 AC 539 ms
43,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
template<typename T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }



int n, m;
vector<vector<pair<int, ll> > > v(100005);

const ll INF = 1e18;
using P = pair<ll, ll>;

vector<ll> dijkstra(int n, vector<vector<pair<int, ll> > > edge, int s) {
    vector<ll> d(n, INF);
    priority_queue<P, vector<P>, greater<P> > que; //頂点、最短距離、小さい順に
    
    
    d[s] = 0;
    que.push(P(s, 0));
    
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int cur = p.F;
        if (d[cur] < p.S) continue;
        rep(i, sz(edge[cur])) {
            int nxt = edge[cur][i].F;
            ll cost = edge[cur][i].S;
            if (d[nxt] > d[cur] + cost) {
                d[nxt] = d[cur] + cost;
                que.push(P(nxt, d[nxt]));
            }
        }
    }
    
    return d;
}

using p2 = pair<int, pair<ll, int> >;
ll d2[100005][2];

void dijkstra2(int n, vector<vector<pair<int, ll> > > edge, int s) {
    rep(i, n) rep(j, 2) d2[i][j] = INF;
    
    priority_queue<p2, vector<p2>, greater<p2> > que; //頂点、最短距離、小さい順に
    
    que.push(p2(s, P(0, 0))); // 頂点, コスト, 0,1
    
    while (!que.empty()) {
        p2 p = que.top(); // 頂点, コスト
        // cout << p.F << " " << p.S.F << " " << p.S.S << endl;
        que.pop();
        if (d2[p.F][p.S.S] < p.S.F) continue;
        
        if (p.S.S == 0) {
            for (auto nxt : v[p.F]) {
                if (chmin(d2[nxt.F][0], p.S.F + nxt.S)) { // 0 -> 0
                    que.push(p2(nxt.F, P(d2[nxt.F][0], 0)));
                }
                // 0 -> 1
                if (chmin(d2[nxt.F][1], p.S.F)) { // 0 -> 1
                    que.push(p2(nxt.F, P(d2[nxt.F][1], 1)));
                }
            }
        } else {
            for (auto nxt : v[p.F]) {
                if (chmin(d2[nxt.F][1], p.S.F + nxt.S)) { // 1 -> 1
                    que.push(p2(nxt.F, P(d2[nxt.F][1], 1)));
                }
                
            }
        }
        
    }
    
}



int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> m;
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        v[a].eb(pii(b, c));
        v[b].eb(pii(a, c));
    }
    
    auto di = dijkstra(n, v, 0);
    // rep(i, n) cout << di[i] << " "; cout << endl;
    dijkstra2(n, v, 0);
    // rep(i, n) cout << d2[i][1] << " "; cout << endl;
    cout << 0 << endl;
    rep3(i, 1, n) cout << di[i] + min(d2[i][0], d2[i][1]) << endl;
}

0