結果

問題 No.2565 はじめてのおつかい
ユーザー shin3110shin3110
提出日時 2023-12-02 15:55:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 3,756 bytes
コンパイル時間 3,923 ms
コンパイル使用メモリ 233,480 KB
実行使用メモリ 9,276 KB
最終ジャッジ日時 2023-12-02 15:55:48
合計ジャッジ時間 6,708 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 49 ms
9,276 KB
testcase_04 AC 33 ms
9,276 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 25 ms
6,548 KB
testcase_07 AC 14 ms
6,548 KB
testcase_08 AC 14 ms
6,548 KB
testcase_09 AC 23 ms
6,548 KB
testcase_10 AC 17 ms
6,548 KB
testcase_11 AC 35 ms
6,548 KB
testcase_12 AC 10 ms
6,548 KB
testcase_13 AC 16 ms
6,548 KB
testcase_14 AC 26 ms
6,548 KB
testcase_15 AC 27 ms
6,548 KB
testcase_16 AC 24 ms
7,480 KB
testcase_17 AC 7 ms
6,548 KB
testcase_18 AC 9 ms
6,548 KB
testcase_19 AC 33 ms
6,548 KB
testcase_20 AC 29 ms
6,548 KB
testcase_21 AC 26 ms
6,548 KB
testcase_22 AC 15 ms
6,548 KB
testcase_23 AC 18 ms
6,548 KB
testcase_24 AC 4 ms
6,548 KB
testcase_25 AC 29 ms
6,548 KB
testcase_26 AC 18 ms
6,548 KB
testcase_27 AC 25 ms
6,548 KB
testcase_28 AC 32 ms
6,548 KB
testcase_29 AC 34 ms
6,952 KB
testcase_30 AC 27 ms
6,548 KB
testcase_31 AC 28 ms
6,548 KB
testcase_32 AC 16 ms
6,548 KB
testcase_33 AC 28 ms
6,548 KB
testcase_34 AC 31 ms
6,548 KB
testcase_35 AC 26 ms
7,008 KB
testcase_36 AC 29 ms
6,548 KB
testcase_37 AC 18 ms
6,548 KB
testcase_38 AC 29 ms
6,548 KB
testcase_39 AC 25 ms
6,548 KB
testcase_40 AC 31 ms
6,896 KB
testcase_41 AC 32 ms
7,188 KB
testcase_42 AC 18 ms
6,548 KB
testcase_43 AC 26 ms
6,548 KB
testcase_44 AC 15 ms
6,548 KB
testcase_45 AC 8 ms
6,548 KB
testcase_46 AC 30 ms
6,548 KB
testcase_47 AC 19 ms
6,548 KB
testcase_48 AC 16 ms
6,548 KB
testcase_49 AC 9 ms
6,548 KB
testcase_50 AC 21 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 17 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*

              ∧∧∩ 
             ( ゚∀゚)/ キタ━━━━━━(゚∀゚)━━━━━━!!!!  
            ⊂   ノ  ゆっくり見てってね!!!!!!!!
             (つ ノ
    o          (ノ
     \      ☆
             |      o
          (⌒ ⌒ヽ   /     ☆
    \  (´⌒  ⌒  ⌒ヾ   /
      ('⌒ ; ⌒   ::⌒  )
     (´     )     ::: ) /
  ☆─ (´⌒;:    ::⌒`) :;  )

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


#define elif else if
#define rep(i, s, n) for (int i=s; i<n; i++)
#define all(a) a.begin(), a.end()
// xを2進数にした際にi桁目が0か1か
#define bit(x,i)((x>>i)&1)
typedef long long ll;
typedef long double ld;

ld pi = 3.14159265358979;

inline void scan(){}
template<class Head,class... Tail>
inline void scan(Head&head,Tail&... tail){std::cin>>head;scan(tail...);}
 
// print(a, b, c) => cout << a << ' ' << b << ' ' << c << endl;
inline void print(){}
template<class Head,class... Tail>
inline void print(Head&head,Tail&... tail){std::cout<<head<<" ";print(tail...);cout<<endl;}

// bool型に対して"Yes", "No" を出力する
bool printb(bool f) {if (f) cout << "Yes\n";else cout << "No\n";return f;}

//vectorのcout,cin
template<typename T>
std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T>
std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}

// 2地点の距離(long double)
long double coor_dist(long double xi, long double yi, long double xj, long double yj) {
    return sqrt(abs((xi - xj) * (xi - xj)) + abs((yi - yj) * (yi - yj)));
}

// 回文かどうかの判定 
bool iskaibun(string s){ll k = s.size();rep(i,0,k/2){if(s[i]!=s[k-1-i]){return false;}}return true;}

template<class T> void chmax(T& a, T b) { if (a < b) a = b; }
template<class T> void chmin(T& a, T b) { if (a > b) a = b; }



#define mod 998244353
using mint = modint998244353;
int op(int a, int b) {return max(a, b);}
int e() {return 0;}
int mapping(int f, int x) {return (f == 1e9 ? x : f);}
int composition(int f, int g) {return (f == 1e9 ? g : f);}
int id() {return 1e9;}


signed main() {
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    vector<vector<int>> side(n);
    rep(i, 0, m) {
        int u, v; cin >> u >> v;
        u--; v--;
        side[u].push_back(v);
    }

    auto f = [&](auto&& f, int s, int g) -> int {
        vector<int> dist(n, -1);
        queue<int> q;
        dist[s] = 0;
        q.push(s);
        while(!q.empty()) {
            int pos = q.front(); q.pop();
            for(int to : side[pos]) {
                if(dist[to] != -1) continue;
                dist[to] = dist[pos]+1;
                q.push(to);
            }
        }
        return dist[g];
    };

    int f_0_a, f_0_b, f_a_b, f_b_a, f_a_0, f_b_0;
    f_0_a = f(f, 0, n-2);
    f_0_b = f(f, 0, n-1);
    f_a_b = f(f, n-2, n-1);
    f_b_a = f(f, n-1, n-2);
    f_a_0 = f(f, n-2, 0);
    f_b_0 = f(f, n-1, 0);
    int ans = 1e9;
    if(f_0_a != -1 && f_a_b != -1 && f_b_0 != -1) ans = f_0_a+f_a_b+f_b_0;
    if(f_0_b != -1 && f_b_a != -1 && f_a_0 != -1) chmin(ans, f_0_b+f_b_a+f_a_0);
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
}
0