結果

問題 No.2565 はじめてのおつかい
ユーザー int_sanint_san
提出日時 2023-12-02 15:49:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,471 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 176,608 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2023-12-02 15:50:04
合計ジャッジ時間 5,776 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 70 ms
9,984 KB
testcase_04 AC 62 ms
9,984 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 46 ms
6,676 KB
testcase_07 AC 24 ms
6,676 KB
testcase_08 AC 23 ms
6,676 KB
testcase_09 AC 43 ms
6,676 KB
testcase_10 AC 27 ms
6,676 KB
testcase_11 AC 62 ms
7,040 KB
testcase_12 AC 17 ms
6,676 KB
testcase_13 AC 28 ms
6,676 KB
testcase_14 AC 45 ms
6,676 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 51 ms
8,192 KB
testcase_17 AC 11 ms
6,676 KB
testcase_18 AC 14 ms
6,676 KB
testcase_19 AC 57 ms
6,676 KB
testcase_20 AC 50 ms
6,676 KB
testcase_21 AC 51 ms
6,676 KB
testcase_22 AC 29 ms
6,676 KB
testcase_23 AC 31 ms
6,676 KB
testcase_24 AC 6 ms
6,676 KB
testcase_25 AC 48 ms
6,676 KB
testcase_26 AC 31 ms
6,676 KB
testcase_27 AC 49 ms
6,676 KB
testcase_28 AC 55 ms
6,676 KB
testcase_29 AC 62 ms
7,424 KB
testcase_30 AC 54 ms
6,912 KB
testcase_31 AC 63 ms
6,676 KB
testcase_32 AC 30 ms
6,676 KB
testcase_33 AC 50 ms
6,784 KB
testcase_34 AC 55 ms
6,676 KB
testcase_35 AC 53 ms
7,552 KB
testcase_36 AC 51 ms
6,784 KB
testcase_37 AC 30 ms
6,676 KB
testcase_38 AC 54 ms
6,676 KB
testcase_39 AC 46 ms
6,676 KB
testcase_40 AC 61 ms
7,424 KB
testcase_41 AC 63 ms
7,680 KB
testcase_42 AC 32 ms
6,676 KB
testcase_43 AC 47 ms
6,676 KB
testcase_44 AC 24 ms
6,676 KB
testcase_45 AC 14 ms
6,676 KB
testcase_46 AC 49 ms
6,676 KB
testcase_47 AC 39 ms
6,676 KB
testcase_48 AC 29 ms
6,676 KB
testcase_49 AC 18 ms
6,676 KB
testcase_50 AC 44 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 38 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
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> q;
    q.push(0);
    vector<int> dist1(N, INF);
    dist1[0] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto next : G[now]) {
            if (dist1[next] == INF) {
                q.push(next);
                dist1[next] = dist1[now] + 1;
            }
        }
    }
    q.push(N - 2);
    vector<int> dist2(N, INF);
    dist2[N - 2] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto next : G[now]) {
            if (dist2[next] == INF) {
                q.push(next);
                dist2[next] = dist2[now] + 1;
            }
        }
    }
    q.push(N - 1);
    vector<int> dist3(N, INF);
    dist3[N - 1] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto next : G[now]) {
            if (dist3[next] == INF) {
                q.push(next);
                dist3[next] = dist3[now] + 1;
            }
        }
    }
    int ans = dist1[N - 2] + dist2[N - 1] + dist3[0];
    ans = min(ans, dist1[N - 1] + dist3[N - 2] + dist2[0]);
    if (ans < INF) {
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
}
0