結果

問題 No.2565 はじめてのおつかい
ユーザー loop0919loop0919
提出日時 2023-10-31 15:32:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,313 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 212,756 KB
実行使用メモリ 11,216 KB
最終ジャッジ日時 2023-10-31 15:32:24
合計ジャッジ時間 6,748 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 91 ms
11,216 KB
testcase_04 AC 71 ms
11,216 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 51 ms
5,320 KB
testcase_07 AC 27 ms
4,380 KB
testcase_08 AC 26 ms
4,732 KB
testcase_09 AC 47 ms
4,976 KB
testcase_10 AC 31 ms
4,880 KB
testcase_11 AC 71 ms
7,784 KB
testcase_12 AC 30 ms
4,348 KB
testcase_13 AC 31 ms
4,700 KB
testcase_14 AC 51 ms
6,308 KB
testcase_15 AC 54 ms
5,936 KB
testcase_16 AC 57 ms
9,368 KB
testcase_17 AC 13 ms
4,348 KB
testcase_18 AC 17 ms
5,672 KB
testcase_19 AC 65 ms
6,992 KB
testcase_20 AC 57 ms
6,728 KB
testcase_21 AC 55 ms
6,992 KB
testcase_22 AC 32 ms
5,672 KB
testcase_23 AC 36 ms
5,672 KB
testcase_24 AC 7 ms
4,348 KB
testcase_25 AC 57 ms
6,992 KB
testcase_26 AC 36 ms
5,408 KB
testcase_27 AC 56 ms
7,256 KB
testcase_28 AC 64 ms
6,992 KB
testcase_29 AC 72 ms
8,312 KB
testcase_30 AC 60 ms
7,784 KB
testcase_31 AC 58 ms
6,992 KB
testcase_32 AC 36 ms
5,408 KB
testcase_33 AC 58 ms
7,520 KB
testcase_34 AC 60 ms
6,600 KB
testcase_35 AC 61 ms
8,312 KB
testcase_36 AC 60 ms
7,520 KB
testcase_37 AC 35 ms
5,672 KB
testcase_38 AC 60 ms
6,464 KB
testcase_39 AC 48 ms
5,936 KB
testcase_40 AC 69 ms
8,048 KB
testcase_41 AC 73 ms
8,612 KB
testcase_42 AC 36 ms
5,208 KB
testcase_43 AC 53 ms
6,200 KB
testcase_44 AC 27 ms
4,880 KB
testcase_45 AC 15 ms
4,352 KB
testcase_46 AC 58 ms
5,936 KB
testcase_47 AC 41 ms
4,584 KB
testcase_48 AC 32 ms
4,728 KB
testcase_49 AC 19 ms
4,348 KB
testcase_50 AC 50 ms
4,592 KB
testcase_51 AC 1 ms
4,348 KB
testcase_52 AC 42 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

const ll INF = 1LL << 60;

vector<ll> bfs(int start, vector<vector<int>> &graph, int N) {
    vector<ll> dists(N, INF);
    dists[start] = 0;

    queue<pair<int, ll>> q;
    q.push(pair<int, ll>(start, 0));

    while (!q.empty()) {
        int now_v = q.front().first;
        ll d = q.front().second;
        q.pop();

        for (int next_v : graph[now_v]) {
            if (dists[next_v] < INF) continue;
            dists[next_v] = d+1;
            q.push(pair<int, ll>(next_v, d+1));
        }
    }
    return dists;
}

int main() {
    int N, M;
    cin >> N >> M;

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

    vector<ll> dist_0 = bfs(0, graph, N);
    vector<ll> dist_N_2 = bfs(N-2, graph, N);
    vector<ll> dist_N_1 = bfs(N-1, graph, N);

    // 町1 -> 町N-1 -> 町N -> 町1 の最短経路の長さ
    ll ans_1 = dist_0[N-2] + dist_N_2[N-1] + dist_N_1[0];

    // 町1 -> 町N -> 町N-1 -> 町1 の最短経路の長さ
    ll ans_2 = dist_0[N-1] + dist_N_1[N-2] + dist_N_2[0];

    ll ans = min(ans_1, ans_2);

    if (ans < INF) cout << ans << endl;
    else cout << -1 << endl;
}
0