結果

問題 No.2565 はじめてのおつかい
ユーザー loop0919loop0919
提出日時 2023-10-31 14:41:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 2,578 ms
コンパイル使用メモリ 212,648 KB
実行使用メモリ 11,204 KB
最終ジャッジ日時 2023-10-31 14:41:57
合計ジャッジ時間 7,198 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 113 ms
11,204 KB
testcase_04 AC 73 ms
11,204 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 56 ms
5,312 KB
testcase_07 AC 29 ms
4,372 KB
testcase_08 AC 28 ms
4,868 KB
testcase_09 AC 54 ms
4,968 KB
testcase_10 AC 33 ms
4,872 KB
testcase_11 AC 78 ms
7,772 KB
testcase_12 AC 19 ms
4,348 KB
testcase_13 AC 32 ms
4,692 KB
testcase_14 AC 54 ms
6,300 KB
testcase_15 AC 58 ms
5,924 KB
testcase_16 AC 62 ms
9,356 KB
testcase_17 AC 14 ms
4,348 KB
testcase_18 AC 18 ms
5,660 KB
testcase_19 AC 73 ms
6,980 KB
testcase_20 AC 64 ms
6,716 KB
testcase_21 AC 60 ms
6,980 KB
testcase_22 AC 35 ms
5,660 KB
testcase_23 AC 41 ms
5,660 KB
testcase_24 AC 8 ms
4,348 KB
testcase_25 AC 62 ms
6,980 KB
testcase_26 AC 40 ms
5,396 KB
testcase_27 AC 64 ms
7,244 KB
testcase_28 AC 69 ms
6,980 KB
testcase_29 AC 78 ms
8,300 KB
testcase_30 AC 65 ms
7,772 KB
testcase_31 AC 63 ms
6,980 KB
testcase_32 AC 36 ms
5,396 KB
testcase_33 AC 63 ms
7,508 KB
testcase_34 AC 65 ms
6,592 KB
testcase_35 AC 66 ms
8,300 KB
testcase_36 AC 64 ms
7,508 KB
testcase_37 AC 38 ms
5,660 KB
testcase_38 AC 66 ms
6,452 KB
testcase_39 AC 52 ms
5,924 KB
testcase_40 AC 76 ms
8,036 KB
testcase_41 AC 80 ms
8,604 KB
testcase_42 AC 39 ms
5,396 KB
testcase_43 AC 61 ms
6,188 KB
testcase_44 AC 28 ms
4,868 KB
testcase_45 AC 17 ms
4,348 KB
testcase_46 AC 63 ms
5,924 KB
testcase_47 AC 45 ms
4,576 KB
testcase_48 AC 33 ms
4,720 KB
testcase_49 AC 19 ms
4,348 KB
testcase_50 AC 53 ms
4,584 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 44 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define ll long long

const ll INF = 1ll << 60;

void bfs(int start, vector<vector<int>> &graph, vector<ll> &dists) {
    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));
        }
    }
}

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(N, INF);
    bfs(0, graph, dist_0);

    vector<ll> dist_N_2(N, INF);
    bfs(N-2, graph, dist_N_2);

    vector<ll> dist_N_1(N, INF);
    bfs(N-1, graph, dist_N_1);

    ll ans = min(
        dist_0[N-2] + dist_N_2[N-1] + dist_N_1[0],
        dist_0[N-1] + dist_N_1[N-2] + dist_N_2[0]
    );

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