結果

問題 No.2565 はじめてのおつかい
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-12-07 14:39:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 973 bytes
コンパイル時間 2,300 ms
コンパイル使用メモリ 210,492 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2023-12-07 14:39:11
合計ジャッジ時間 5,703 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 48 ms
11,008 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 24 ms
6,676 KB
testcase_07 AC 13 ms
6,676 KB
testcase_08 AC 14 ms
6,676 KB
testcase_09 AC 22 ms
6,676 KB
testcase_10 AC 16 ms
6,676 KB
testcase_11 AC 36 ms
7,680 KB
testcase_12 AC 9 ms
6,676 KB
testcase_13 AC 15 ms
6,676 KB
testcase_14 AC 25 ms
6,676 KB
testcase_15 AC 25 ms
6,676 KB
testcase_16 AC 28 ms
9,216 KB
testcase_17 AC 7 ms
6,676 KB
testcase_18 AC 9 ms
6,676 KB
testcase_19 AC 30 ms
7,040 KB
testcase_20 AC 28 ms
6,676 KB
testcase_21 AC 26 ms
6,784 KB
testcase_22 AC 15 ms
6,676 KB
testcase_23 AC 18 ms
6,676 KB
testcase_24 AC 5 ms
6,676 KB
testcase_25 AC 28 ms
6,784 KB
testcase_26 AC 18 ms
6,676 KB
testcase_27 AC 27 ms
7,168 KB
testcase_28 AC 32 ms
6,784 KB
testcase_29 AC 35 ms
8,064 KB
testcase_30 AC 29 ms
7,552 KB
testcase_31 AC 28 ms
6,912 KB
testcase_32 AC 17 ms
6,676 KB
testcase_33 AC 29 ms
7,296 KB
testcase_34 AC 30 ms
6,676 KB
testcase_35 AC 30 ms
8,320 KB
testcase_36 AC 30 ms
7,296 KB
testcase_37 AC 18 ms
6,676 KB
testcase_38 AC 28 ms
6,676 KB
testcase_39 AC 24 ms
6,676 KB
testcase_40 AC 34 ms
8,064 KB
testcase_41 AC 34 ms
8,576 KB
testcase_42 AC 18 ms
6,676 KB
testcase_43 AC 25 ms
6,676 KB
testcase_44 AC 14 ms
6,676 KB
testcase_45 AC 8 ms
6,676 KB
testcase_46 AC 28 ms
6,676 KB
testcase_47 AC 18 ms
6,676 KB
testcase_48 AC 16 ms
6,676 KB
testcase_49 AC 9 ms
6,676 KB
testcase_50 AC 21 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 17 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<ll> bfs(vector<vector<int>> &E, int start){

    int N = E.size();
    int from, to;
    queue<int> que;
    vector<ll> dist(N, 1e9);

    dist[start] = 0;
    que.push(start);

    while(!que.empty()){
        from = que.front();
        que.pop();
        for (auto to : E[from]){
            if (dist[to] != 1e9) continue;
            dist[to] = dist[from] + 1;
            que.push(to);
        }
    }

    return dist;
}

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

    int N, M, A, B;
    ll ans;
    cin >> N >> M;
    vector<vector<int>> E(N);

    for (int i=0; i<M; i++){
        cin >> A >> B; A--; B--;
        E[A].push_back(B);
    }

    vector<ll> dist1 = bfs(E, 0), dist2 = bfs(E, N-2), dist3 = bfs(E, N-1);
    ans = min(dist1[N-2]+dist2[N-1]+dist3[0], dist1[N-1]+dist3[N-2]+dist2[0]);

    cout << (ans < 1e9 ? ans : -1) << endl;

    return 0;
}
0