結果

問題 No.2565 はじめてのおつかい
ユーザー miiitomimiiitomi
提出日時 2023-12-02 15:46:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 825 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 211,680 KB
実行使用メモリ 15,456 KB
最終ジャッジ日時 2023-12-02 15:46:22
合計ジャッジ時間 6,448 ms
ジャッジサーバーID
(参考情報)
judge10 / 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 115 ms
15,456 KB
testcase_04 AC 89 ms
15,456 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 55 ms
6,676 KB
testcase_07 AC 30 ms
6,676 KB
testcase_08 AC 30 ms
6,676 KB
testcase_09 AC 51 ms
6,676 KB
testcase_10 AC 37 ms
6,676 KB
testcase_11 AC 81 ms
9,920 KB
testcase_12 AC 20 ms
6,676 KB
testcase_13 AC 34 ms
6,676 KB
testcase_14 AC 58 ms
7,720 KB
testcase_15 AC 59 ms
7,036 KB
testcase_16 AC 71 ms
12,016 KB
testcase_17 AC 16 ms
6,676 KB
testcase_18 AC 22 ms
6,676 KB
testcase_19 AC 73 ms
9,012 KB
testcase_20 AC 65 ms
8,540 KB
testcase_21 AC 64 ms
8,600 KB
testcase_22 AC 38 ms
6,940 KB
testcase_23 AC 42 ms
6,812 KB
testcase_24 AC 9 ms
6,676 KB
testcase_25 AC 64 ms
8,792 KB
testcase_26 AC 41 ms
6,676 KB
testcase_27 AC 65 ms
9,164 KB
testcase_28 AC 72 ms
8,732 KB
testcase_29 AC 83 ms
10,624 KB
testcase_30 AC 71 ms
9,896 KB
testcase_31 AC 67 ms
8,860 KB
testcase_32 AC 39 ms
6,676 KB
testcase_33 AC 69 ms
9,548 KB
testcase_34 AC 66 ms
7,952 KB
testcase_35 AC 72 ms
10,908 KB
testcase_36 AC 68 ms
9,408 KB
testcase_37 AC 41 ms
6,716 KB
testcase_38 AC 66 ms
8,084 KB
testcase_39 AC 54 ms
7,188 KB
testcase_40 AC 82 ms
10,544 KB
testcase_41 AC 84 ms
11,228 KB
testcase_42 AC 41 ms
6,676 KB
testcase_43 AC 60 ms
7,688 KB
testcase_44 AC 31 ms
6,676 KB
testcase_45 AC 17 ms
6,676 KB
testcase_46 AC 65 ms
7,308 KB
testcase_47 AC 43 ms
6,676 KB
testcase_48 AC 35 ms
6,676 KB
testcase_49 AC 20 ms
6,676 KB
testcase_50 AC 52 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 43 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N, M;
vector<vector<int>> G;

vector<int> f(int s, vector<vector<int>> G) {
    vector<int> d(N, 1e+8);
    d[s] = 0;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
        int v = Q.front();
        Q.pop();
        for (int w : G[v]) {
            if (d[w] < (int)1e+8) continue;
            d[w] = d[v] + 1;
            Q.push(w);
        }
    }
    return d;
}

int main() {
    cin >> N >> M;
    G.resize(N);
    for (int i = 0; i < M; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
    }

    vector<int> d0 = f(0, G), dN1 = f(N-2, G), dN = f(N-1, G);

    int ans = min(d0[N-2] +dN1[N-1] + dN[0], d0[N-1]+dN[N-2]+dN1[0]);
    if (ans >= (int)1e+8) cout << -1 << endl;
    else cout << ans << endl;
}
0