結果

問題 No.2565 はじめてのおつかい
ユーザー suminoeno7suminoeno7
提出日時 2023-12-02 18:02:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,536 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2023-12-02 18:02:15
合計ジャッジ時間 5,220 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 76 ms
9,984 KB
testcase_04 AC 67 ms
9,984 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 51 ms
6,676 KB
testcase_07 AC 26 ms
6,676 KB
testcase_08 AC 25 ms
6,676 KB
testcase_09 AC 46 ms
6,676 KB
testcase_10 AC 30 ms
6,676 KB
testcase_11 AC 66 ms
7,040 KB
testcase_12 AC 19 ms
6,676 KB
testcase_13 AC 30 ms
6,676 KB
testcase_14 AC 48 ms
6,676 KB
testcase_15 AC 51 ms
6,676 KB
testcase_16 AC 66 ms
8,192 KB
testcase_17 AC 13 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 63 ms
6,676 KB
testcase_20 AC 54 ms
6,676 KB
testcase_21 AC 53 ms
6,676 KB
testcase_22 AC 31 ms
6,676 KB
testcase_23 AC 35 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 54 ms
6,676 KB
testcase_26 AC 35 ms
6,676 KB
testcase_27 AC 53 ms
6,676 KB
testcase_28 AC 62 ms
6,676 KB
testcase_29 AC 68 ms
7,424 KB
testcase_30 AC 57 ms
6,912 KB
testcase_31 AC 57 ms
6,676 KB
testcase_32 AC 32 ms
6,676 KB
testcase_33 AC 56 ms
6,784 KB
testcase_34 AC 58 ms
6,676 KB
testcase_35 AC 58 ms
7,552 KB
testcase_36 AC 70 ms
6,784 KB
testcase_37 AC 34 ms
6,676 KB
testcase_38 AC 57 ms
6,676 KB
testcase_39 AC 47 ms
6,676 KB
testcase_40 AC 66 ms
7,424 KB
testcase_41 AC 67 ms
7,680 KB
testcase_42 AC 36 ms
6,676 KB
testcase_43 AC 51 ms
6,676 KB
testcase_44 AC 26 ms
6,676 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 56 ms
6,676 KB
testcase_47 AC 41 ms
6,676 KB
testcase_48 AC 31 ms
6,676 KB
testcase_49 AC 18 ms
6,676 KB
testcase_50 AC 50 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 42 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> G(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
    }
    queue<int> test;
    vector<int> dis1(n, -1), dis2(n, -1), dis3(n, -1);
    dis1[0] = 0;
    test.push(0);
    while(!test.empty()) {
        int p = test.front();
        test.pop();
        for (int w : G[p]) {
            if (dis1[w] != -1) continue;
            dis1[w] = dis1[p] + 1;
            test.push(w);
        }
    }
    dis2[n - 2] = 0;
    test.push(n - 2);
    while(!test.empty()) {
        int p = test.front();
        test.pop();
        for (int w : G[p]) {
            if (dis2[w] != -1) continue;
            dis2[w] = dis2[p] + 1;
            test.push(w);
        }
    }
    dis3[n - 1] = 0;
    test.push(n - 1);
    while(!test.empty()) {
        int p = test.front();
        test.pop();
        for (int w : G[p]) {
            if (dis3[w] != -1) continue;
            dis3[w] = dis3[p] + 1;
            test.push(w);
        }
    }
    int res = -1;
    if (dis1[n - 2] != -1 && dis2[n - 1] != -1 && dis3[0] != -1) res = dis1[n - 2] + dis2[n - 1] + dis3[0];
    if (dis1[n - 1] != -1 && dis3[n - 2] != -1 && dis2[0] != -1) {
        if (res == -1) res = dis1[n - 1] + dis3[n - 2] + dis2[0];
        else res = min(res, dis1[n - 1] + dis3[n - 2] + dis2[0]);
    }
    cout << res << endl;
    return 0;
}
0