結果

問題 No.807 umg tours
ユーザー minatominato
提出日時 2020-03-16 22:45:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 4,000 ms
コード長 1,961 bytes
コンパイル時間 2,454 ms
コンパイル使用メモリ 196,188 KB
実行使用メモリ 24,356 KB
最終ジャッジ日時 2024-05-03 00:13:18
合計ジャッジ時間 6,876 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_07 AC 2 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 142 ms
16,176 KB
testcase_12 AC 156 ms
14,400 KB
testcase_13 AC 208 ms
17,612 KB
testcase_14 AC 86 ms
9,740 KB
testcase_15 AC 60 ms
8,064 KB
testcase_16 AC 218 ms
18,440 KB
testcase_17 AC 285 ms
23,628 KB
testcase_18 AC 283 ms
23,532 KB
testcase_19 AC 254 ms
20,368 KB
testcase_20 AC 130 ms
12,628 KB
testcase_21 AC 140 ms
12,928 KB
testcase_22 AC 45 ms
7,424 KB
testcase_23 AC 38 ms
6,528 KB
testcase_24 AC 99 ms
21,148 KB
testcase_25 AC 277 ms
24,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
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; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T>
struct edge {
    int src,to;
    T cost;

    edge() = default;
    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
    bool operator<(const edge &e) const {
        return cost < e.cost;
    }
};

ll dist[101010][2];

using Tu = tuple<ll, ll, ll>;
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M; cin >> N >> M;
    vector<vector<edge<ll>>> G(N);
    rep(i,M) {
        int a,b,c; cin >> a >> b >> c;
        --a; --b;
        G[a].emplace_back(b,c);
        G[b].emplace_back(a,c);
    }

    priority_queue<Tu, vector<Tu>, greater<Tu>> pq;
    pq.emplace(make_tuple(0,0,0));
    rep(i,N) rep(j,2) dist[i][j] = 1e18;
    dist[0][0] = dist[0][1] = 0;
    while (!pq.empty()) {
        ll d,v,n;
        tie(d,v,n) = pq.top(); pq.pop();
        if (d > dist[v][n]) continue;
        for (auto &e : G[v]) {
            if (chmin(dist[e.to][n],dist[v][n] + e.cost)) {
                pq.emplace(make_tuple(dist[e.to][n],e.to,n));
            }
            if (n == 0 && chmin(dist[e.to][1],dist[v][0])) {
                pq.emplace(make_tuple(dist[e.to][1],e.to,1));
            }
        }
    }

    rep(i,N) {
        cout << dist[i][0] + dist[i][1] << ln;
    }
}
0