結果

問題 No.2565 はじめてのおつかい
ユーザー Mikan04yMikan04y
提出日時 2023-12-02 16:59:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,996 bytes
コンパイル時間 2,484 ms
コンパイル使用メモリ 210,248 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2023-12-02 16:59:37
合計ジャッジ時間 6,958 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 3 ms
6,548 KB
testcase_03 AC 89 ms
9,216 KB
testcase_04 AC 68 ms
9,216 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 52 ms
6,548 KB
testcase_07 AC 28 ms
6,548 KB
testcase_08 AC 26 ms
6,548 KB
testcase_09 AC 47 ms
6,548 KB
testcase_10 AC 32 ms
6,548 KB
testcase_11 AC 75 ms
6,548 KB
testcase_12 AC 19 ms
6,548 KB
testcase_13 AC 32 ms
6,548 KB
testcase_14 AC 52 ms
6,548 KB
testcase_15 AC 55 ms
6,548 KB
testcase_16 AC 60 ms
7,552 KB
testcase_17 AC 13 ms
6,548 KB
testcase_18 AC 17 ms
6,548 KB
testcase_19 AC 68 ms
6,548 KB
testcase_20 AC 60 ms
6,548 KB
testcase_21 AC 59 ms
6,548 KB
testcase_22 AC 33 ms
6,548 KB
testcase_23 AC 38 ms
6,548 KB
testcase_24 AC 7 ms
6,548 KB
testcase_25 AC 59 ms
6,548 KB
testcase_26 AC 37 ms
6,548 KB
testcase_27 AC 60 ms
6,548 KB
testcase_28 AC 69 ms
6,548 KB
testcase_29 AC 78 ms
6,912 KB
testcase_30 AC 64 ms
6,548 KB
testcase_31 AC 62 ms
6,548 KB
testcase_32 AC 35 ms
6,548 KB
testcase_33 AC 63 ms
6,548 KB
testcase_34 AC 65 ms
6,548 KB
testcase_35 AC 65 ms
6,912 KB
testcase_36 AC 63 ms
6,548 KB
testcase_37 AC 37 ms
6,548 KB
testcase_38 AC 64 ms
6,548 KB
testcase_39 AC 51 ms
6,548 KB
testcase_40 AC 76 ms
6,784 KB
testcase_41 AC 75 ms
7,168 KB
testcase_42 AC 38 ms
6,548 KB
testcase_43 AC 54 ms
6,548 KB
testcase_44 AC 27 ms
6,548 KB
testcase_45 AC 16 ms
6,548 KB
testcase_46 AC 61 ms
6,548 KB
testcase_47 AC 41 ms
6,548 KB
testcase_48 AC 32 ms
6,548 KB
testcase_49 AC 19 ms
6,548 KB
testcase_50 AC 50 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 42 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100'009;
int N;
int visited[MAX_N];

void v_clear()
{
    for (int i = 0; i < N; i++)
        visited[i] = -1;
}

void bfs(vector<vector<int>> &Graph, queue<int> &Q, int first)
{
    v_clear();
    visited[first] = 0;
    Q.push(first);
    while (!Q.empty())
    {
        int place = Q.front();
        Q.pop();
        for (int next : Graph[place])
        {
            if (visited[next] != -1)
                continue;
            visited[next] = visited[place] + 1;
            Q.push(next);
        }
    }
}

int main()
{
    int M;
    cin >> N >> M;
    vector<vector<int>> Graph(N, vector<int>(0));
    for (int i = 0; i < M; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        Graph[u].push_back(v);
    }
    queue<int> Q;
    int len1toN, len1toN_bfr, lenN_bfrto1, lenN_bfrtoN, lenNto1, lenNtoN_bfr;
    // 1 -> N-1, 1 -> N
    bfs(Graph, Q, 0);
    len1toN = visited[N - 1];
    len1toN_bfr = visited[N - 2];
    // N-1 -> 1, N-1 -> N
    bfs(Graph, Q, N - 2);
    lenN_bfrto1 = visited[0];
    lenN_bfrtoN = visited[N-1];
    // N -> 1, N -> N-1
    bfs(Graph, Q, N - 1);
    lenNto1 = visited[0];
    lenNtoN_bfr = visited[N - 2];

    // cout << len1toN << endl;
    // cout << len1toN_bfr << endl;
    // cout << lenN_bfrto1 << endl;
    // cout << lenN_bfrtoN << endl;
    // cout << lenNto1 << endl;
    // cout << lenNtoN_bfr << endl;

    int path1, path2;
    if (len1toN_bfr == -1 || lenN_bfrtoN == -1 || lenNto1 == -1)
        path1 = -1;
    else
        path1 = len1toN_bfr + lenN_bfrtoN + lenNto1;
    if (len1toN == -1 || lenNtoN_bfr == -1 || lenN_bfrto1 == -1)
        path2 = -1;
    else
        path2 = len1toN + lenNtoN_bfr + lenN_bfrto1;
    if (path1 == -1 && path2 == -1)
        cout << -1 << endl;
    else if (path1 == -1)
        cout << path2 << endl;
    else if (path2 == -1)
        cout << path1 << endl;
    else
        cout << min(path1, path2) << endl;
}
0