結果

問題 No.2565 はじめてのおつかい
ユーザー GOTKAKOGOTKAKO
提出日時 2023-12-02 15:18:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 1,197 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 216,540 KB
実行使用メモリ 28,404 KB
最終ジャッジ日時 2023-12-02 15:18:43
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 117 ms
28,404 KB
testcase_04 AC 61 ms
28,404 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 52 ms
10,880 KB
testcase_07 AC 26 ms
7,424 KB
testcase_08 AC 28 ms
8,448 KB
testcase_09 AC 43 ms
9,856 KB
testcase_10 AC 34 ms
9,088 KB
testcase_11 AC 96 ms
18,264 KB
testcase_12 AC 14 ms
6,548 KB
testcase_13 AC 34 ms
8,576 KB
testcase_14 AC 61 ms
13,672 KB
testcase_15 AC 63 ms
12,800 KB
testcase_16 AC 51 ms
21,796 KB
testcase_17 AC 11 ms
6,548 KB
testcase_18 AC 18 ms
10,324 KB
testcase_19 AC 68 ms
16,380 KB
testcase_20 AC 55 ms
15,168 KB
testcase_21 AC 47 ms
15,388 KB
testcase_22 AC 26 ms
11,136 KB
testcase_23 AC 37 ms
11,264 KB
testcase_24 AC 5 ms
6,548 KB
testcase_25 AC 54 ms
15,420 KB
testcase_26 AC 34 ms
10,624 KB
testcase_27 AC 44 ms
16,112 KB
testcase_28 AC 58 ms
15,880 KB
testcase_29 AC 80 ms
19,452 KB
testcase_30 AC 49 ms
17,524 KB
testcase_31 AC 67 ms
15,800 KB
testcase_32 AC 28 ms
10,496 KB
testcase_33 AC 47 ms
16,804 KB
testcase_34 AC 56 ms
14,316 KB
testcase_35 AC 56 ms
19,620 KB
testcase_36 AC 60 ms
16,768 KB
testcase_37 AC 33 ms
11,008 KB
testcase_38 AC 60 ms
14,340 KB
testcase_39 AC 60 ms
12,544 KB
testcase_40 AC 60 ms
19,236 KB
testcase_41 AC 64 ms
20,624 KB
testcase_42 AC 36 ms
10,368 KB
testcase_43 AC 38 ms
13,416 KB
testcase_44 AC 26 ms
8,448 KB
testcase_45 AC 11 ms
6,656 KB
testcase_46 AC 70 ms
13,312 KB
testcase_47 AC 35 ms
8,704 KB
testcase_48 AC 33 ms
8,832 KB
testcase_49 AC 13 ms
6,548 KB
testcase_50 AC 37 ms
9,216 KB
testcase_51 AC 1 ms
6,548 KB
testcase_52 AC 20 ms
8,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<vector<pair<int,int>>> Graph(4*N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        for(int k=0; k<4; k++){
            Graph.at(u).push_back({v,1});
            u += N, v += N;
        }
    }

    Graph.at(N-2).push_back({N-2+N,0});
    Graph.at(N-2+2*N).push_back({N-2+3*N,0});
    Graph.at(N-1).push_back({N-1+2*N,0});
    Graph.at(N-1+N).push_back({N-1+3*N,0});

    vector<bool> already(4*N);
    vector<long long> dist(4*N,2e18);
    priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;
    Q.push({0,0}); dist.at(0) = 0;
    while(Q.size()){
        auto[pdist,pos] = Q.top(); Q.pop();
        if(already.at(pos)) continue;
        already.at(pos) = true;
        
        for(auto [to,cost] : Graph.at(pos)){
            if(dist.at(to) > pdist+cost){
                dist.at(to) = pdist+cost;
                Q.push({dist.at(to),to});
            }
        }
    }

    if(dist.at(3*N) == 2e18) dist.at(3*N) = -1;
    cout << dist.at(3*N) << endl;
}
0