結果

問題 No.2569 はじめてのおつかいHard
ユーザー ragnaragna
提出日時 2023-11-13 23:37:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 1,154 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 211,584 KB
実行使用メモリ 27,484 KB
最終ジャッジ日時 2023-11-13 23:37:56
合計ジャッジ時間 8,149 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 286 ms
20,204 KB
testcase_01 AC 260 ms
20,204 KB
testcase_02 AC 260 ms
20,204 KB
testcase_03 AC 261 ms
20,204 KB
testcase_04 AC 263 ms
20,204 KB
testcase_05 AC 538 ms
27,148 KB
testcase_06 AC 436 ms
23,776 KB
testcase_07 AC 500 ms
27,180 KB
testcase_08 AC 519 ms
27,484 KB
testcase_09 AC 392 ms
24,944 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using Pair=pair<ll,ll>;
const ll INF=1e18;

void dijkstra(ll s,vector<vector<Pair>> &G,vector<ll> &dist){
    priority_queue<Pair,vector<Pair>,greater<Pair>> que;
    que.push({0,s});
    while(!que.empty()){
        ll to=que.top().second,cost=que.top().first;
        if(cost<dist[to]){
            dist[to]=cost;
            for(auto&& [x,y]:G[to]){
                que.push({dist[to]+y,x});
            }
        }
        que.pop();
    }
}

int main(){
    ll N,M; cin >> N >> M;
    vector<vector<Pair>> G1(N),G2(M);
    for(int i=0;i<M;i++){
        ll u,v,t; cin >> u >> v >> t;
        u--,v--;
        G1[u].push_back({v,t});
        G2[v].push_back({u,t});
    }
    vector<ll> dist1(N,INF),dist2(N,INF),dist3(N,INF),dist4(N,INF);
    dijkstra(N-2,G1,dist1);
    dijkstra(N-2,G2,dist2);
    dijkstra(N-1,G1,dist3);
    dijkstra(N-1,G2,dist4);
    for(int i=0;i<N-2;i++){
        ll ans=min(dist2[i]+dist1[N-1]+dist3[i],dist4[i]+dist3[N-2]+dist1[i]);
        if(ans<INF){
            cout << ans << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
}
0