結果

問題 No.2565 はじめてのおつかい
ユーザー SpicaSpica
提出日時 2023-12-02 16:27:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 3,052 bytes
コンパイル時間 4,532 ms
コンパイル使用メモリ 267,332 KB
実行使用メモリ 9,252 KB
最終ジャッジ日時 2023-12-02 16:27:54
合計ジャッジ時間 8,660 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 95 ms
9,252 KB
testcase_04 AC 70 ms
9,252 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 54 ms
6,676 KB
testcase_07 AC 28 ms
6,676 KB
testcase_08 AC 27 ms
6,676 KB
testcase_09 AC 49 ms
6,676 KB
testcase_10 AC 33 ms
6,676 KB
testcase_11 AC 73 ms
6,676 KB
testcase_12 AC 20 ms
6,676 KB
testcase_13 AC 33 ms
6,676 KB
testcase_14 AC 52 ms
6,676 KB
testcase_15 AC 56 ms
6,676 KB
testcase_16 AC 56 ms
7,552 KB
testcase_17 AC 13 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 64 ms
6,676 KB
testcase_20 AC 56 ms
6,676 KB
testcase_21 AC 54 ms
6,676 KB
testcase_22 AC 30 ms
6,676 KB
testcase_23 AC 36 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 59 ms
6,676 KB
testcase_26 AC 36 ms
6,676 KB
testcase_27 AC 53 ms
6,676 KB
testcase_28 AC 62 ms
6,676 KB
testcase_29 AC 70 ms
6,928 KB
testcase_30 AC 56 ms
6,676 KB
testcase_31 AC 58 ms
6,676 KB
testcase_32 AC 32 ms
6,676 KB
testcase_33 AC 56 ms
6,676 KB
testcase_34 AC 58 ms
6,676 KB
testcase_35 AC 58 ms
6,984 KB
testcase_36 AC 58 ms
6,676 KB
testcase_37 AC 35 ms
6,676 KB
testcase_38 AC 54 ms
6,676 KB
testcase_39 AC 51 ms
6,676 KB
testcase_40 AC 67 ms
6,872 KB
testcase_41 AC 69 ms
7,168 KB
testcase_42 AC 38 ms
6,676 KB
testcase_43 AC 49 ms
6,676 KB
testcase_44 AC 27 ms
6,676 KB
testcase_45 AC 14 ms
6,676 KB
testcase_46 AC 60 ms
6,676 KB
testcase_47 AC 41 ms
6,676 KB
testcase_48 AC 33 ms
6,676 KB
testcase_49 AC 19 ms
6,676 KB
testcase_50 AC 50 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 42 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long; // -2^63 ~ 2^63-1 (9.2*10^18)
using ull = unsigned long long; // 0 ~ 2^64-1 (1.8*10^19)
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for (int i = (m); (i) < int (n); i++)
#define all(v) v.begin(), v.end()
#define bit(n) (1ll<<(n)) // 2^n
#define sz(x) ((int)(x).size())
#define fi first
#define se second
template<class T> using maxpq = priority_queue<T>;
template<class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll llINF = 1000000000000000000;
void YesNo(bool flag){
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
}
// 多次元vector生成
template<ll idx = 0, ll n, class T>
auto make_vec(const ll (&d)[n], T&& init) noexcept {
    if constexpr (idx < n) return std::vector(d[idx], make_vec<idx + 1>(d, std::forward<T>(init)));
    else return init;
}
template<class T, ll idx = 0, ll n>
auto make_vec(const ll (&d)[n], const T& init = {}) noexcept {
    if constexpr (idx < n) return std::vector(d[idx], make_vec<idx + 1>(d, init));
    else return init;
}
// auto 変数名 = make_vec<型>({a, b, ...}, 初期値);

int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    rep(i, M){
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
    }

    vector<vector<int>> s = {{0, N-2, N-1}, {0, N-1, N-2}};
    vector<vector<int>> g = {{N-2, N-1, 0}, {N-1, N-2, 0}};

    vector<int> ans(2, 1e7);
    rep(i, 2){
        int tmp = 0;
        rep(j, 3){
            vector<int> dist(N, -1); // 全頂点を「未訪問」に初期化
            queue<int> que;

            dist[s[i][j]] = 0;
            que.push(s[i][j]);

            while (!que.empty()) {
                int v = que.front(); // キューから先頭頂点を取り出す
                que.pop();

                // v から辿れる頂点をすべて調べる
                for (int nv : G[v]) {
                    if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

                    // 新たな白色頂点 nv について距離情報を更新してキューに追加する
                    dist[nv] = dist[v] + 1;
                    que.push(nv);
                }
            }

            if(dist[g[i][j]] == -1){
                break;
            }
            else{
                tmp += dist[g[i][j]];
                //cout << dist[g[i][j]] << " ";
                if(j == 2){
                    ans[i] = tmp;
                    //cout << endl;
                }
            }
        }
    }

    if(ans[0] == 1e7 && ans[1] == 1e7){
        cout << -1 << endl;
    }
    else{
        cout << min(ans[0], ans[1]) << endl;
    }

    //cout << ans[0] << " " << ans[1] << endl;
}
0