結果

問題 No.807 umg tours
ユーザー Manuel1024Manuel1024
提出日時 2021-05-13 23:24:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 632 ms / 4,000 ms
コード長 1,519 bytes
コンパイル時間 3,212 ms
コンパイル使用メモリ 80,468 KB
実行使用メモリ 31,944 KB
最終ジャッジ日時 2023-10-26 03:19:47
合計ジャッジ時間 9,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 305 ms
21,220 KB
testcase_12 AC 339 ms
19,412 KB
testcase_13 AC 450 ms
23,792 KB
testcase_14 AC 182 ms
12,416 KB
testcase_15 AC 135 ms
10,440 KB
testcase_16 AC 463 ms
25,252 KB
testcase_17 AC 632 ms
30,964 KB
testcase_18 AC 610 ms
31,008 KB
testcase_19 AC 591 ms
27,380 KB
testcase_20 AC 333 ms
17,644 KB
testcase_21 AC 352 ms
18,172 KB
testcase_22 AC 133 ms
9,936 KB
testcase_23 AC 108 ms
8,400 KB
testcase_24 AC 312 ms
24,392 KB
testcase_25 AC 631 ms
31,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long int;
constexpr ll INF = 1LL << 60;

struct Node{
    int to;
    ll cost;
    int isused;

    bool operator>(const Node &other) const {
        if(this->cost != other.cost) return this->cost > other.cost;
        else return this->isused > other.isused;
    }
};

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<Node>> G(n);
    for(int i = 0; i < m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b, c, 0});
        G[b].push_back({a, c, 0});
    }

    vector<vector<ll>> dist(n, vector<ll>(2, INF));
    dist[0][0] = 0;
    dist[0][1] = 0;
    priority_queue<Node, vector<Node>, greater<Node>> Q;
    Q.push({0, 0, 0});
    while(Q.size()){
        auto ccur = Q.top(); Q.pop();
        int cur = ccur.to;
        ll ccost = ccur.cost;
        int isused = ccur.isused;

        if(ccost > dist[cur][isused]) continue;
        for(auto &p: G[cur]){
            if(isused == 0 && dist[p.to][1] > dist[cur][isused]){
                dist[p.to][1] = dist[cur][isused];
                Q.push({p.to, dist[p.to][1], 1});
            }
            if(dist[p.to][isused] > dist[cur][isused] + p.cost){
                dist[p.to][isused] = dist[cur][isused] + p.cost;
                Q.push({p.to, dist[p.to][isused], isused});
            }
        }
    }

    for(int i = 0; i < n; i++){
        cout << dist[i][0] + dist[i][1] << endl;
    }
    return 0;
}
0