結果

問題 No.2565 はじめてのおつかい
ユーザー *yudai25n*yudai25n
提出日時 2023-12-02 16:31:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 3,365 bytes
コンパイル時間 6,067 ms
コンパイル使用メモリ 266,816 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2023-12-02 16:31:54
合計ジャッジ時間 9,389 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 80 ms
11,136 KB
testcase_04 AC 69 ms
11,136 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 51 ms
6,676 KB
testcase_07 AC 27 ms
6,676 KB
testcase_08 AC 26 ms
6,676 KB
testcase_09 AC 47 ms
6,676 KB
testcase_10 AC 56 ms
6,676 KB
testcase_11 AC 69 ms
7,808 KB
testcase_12 AC 19 ms
6,676 KB
testcase_13 AC 32 ms
6,676 KB
testcase_14 AC 49 ms
6,676 KB
testcase_15 AC 52 ms
6,676 KB
testcase_16 AC 55 ms
9,344 KB
testcase_17 AC 12 ms
6,676 KB
testcase_18 AC 16 ms
6,676 KB
testcase_19 AC 62 ms
7,040 KB
testcase_20 AC 55 ms
6,784 KB
testcase_21 AC 54 ms
6,912 KB
testcase_22 AC 30 ms
6,676 KB
testcase_23 AC 36 ms
6,676 KB
testcase_24 AC 8 ms
6,676 KB
testcase_25 AC 55 ms
6,912 KB
testcase_26 AC 35 ms
6,676 KB
testcase_27 AC 55 ms
7,296 KB
testcase_28 AC 62 ms
6,912 KB
testcase_29 AC 70 ms
8,192 KB
testcase_30 AC 59 ms
7,680 KB
testcase_31 AC 57 ms
7,040 KB
testcase_32 AC 33 ms
6,676 KB
testcase_33 AC 62 ms
7,424 KB
testcase_34 AC 61 ms
6,676 KB
testcase_35 AC 64 ms
8,448 KB
testcase_36 AC 63 ms
7,424 KB
testcase_37 AC 37 ms
6,676 KB
testcase_38 AC 63 ms
6,676 KB
testcase_39 AC 48 ms
6,676 KB
testcase_40 AC 68 ms
8,192 KB
testcase_41 AC 70 ms
8,576 KB
testcase_42 AC 37 ms
6,676 KB
testcase_43 AC 53 ms
6,676 KB
testcase_44 AC 27 ms
6,676 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 57 ms
6,676 KB
testcase_47 AC 41 ms
6,676 KB
testcase_48 AC 31 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 46 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i,n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i,l,r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using LP = pair<long long,long long>;
using SP = pair<string,string>;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using mint1 = modint1000000007;
using mint9 = modint998244353;
const int INF = 1e9;
const long long LINF = 1e18;
const int di[] = {0,1,0,-1,-1,1,-1,1};
const int dj[] = {1,0,-1,0,1,1,-1,-1};
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
void YesNo (bool ok , const string &s , const string &t) {if(ok) cout << s << endl; else cout << t << endl;} 


//-----def end-----//

 class UGraph {
     private:
         vector<long long> dist_bfs (int s) {
             vector<long long> res(n , LINF);
             queue<int> qe;
             qe.push(s);
             res[s] = 0;
             while(!qe.empty()) {
                 int pos = qe.front(); qe.pop();
                 for(int u : g[pos]) {
                     if(res[u] != LINF) continue;
                     res[u] = res[pos]+1;
                     qe.push(u);
                 }
             }
             return res;
         }
         bool bipartite_bfs () {
             vector<int> color(n , -1);
             bool isBipartite = true;
             for(int s = 0; s < n; s++) {
                 if(color[s] != -1) continue;
                 queue<int> qe;
                 qe.push(s);
                 color[s] = 0;
                 while(!qe.empty()) {
                     int pos = qe.front(); qe.pop();
                     for(int u : g[pos]) {
                         if(color[u] != -1) {
                             if(color[u] == color[pos]) isBipartite = false;
                             continue;
                         }
                         color[u] = 1-color[pos];
                         qe.push(u);
                     }
                 }
             }
             return isBipartite;
         }
     public:
         int n , m;
         vector<vector<int>> g;
         UGraph (int nn) {
             g = vector<vector<int>>(nn);
             n = nn;
         }
         void merge (int u , int v) {
             g[u].push_back(v);
            //g[v].push_back(u);
         }
         vector<long long> dist(int s) {
             return dist_bfs(s);
         }
         bool bipartite () {
             return bipartite_bfs();
         }
 };
 


int main () {
    int n , m;
    cin >> n >> m;
    UGraph g(n);
    rep(i , m) {
        int u , v;
        cin >> u >> v;
        u--; v--;
        g.merge(u , v);
    }
    auto d0 = g.dist(0) , dn2 = g.dist(n-2) , dn1 = g.dist(n-1);
    long long res = LINF;
    chmin(res , d0[n-2]+dn2[n-1]+dn1[0]);
    chmin(res , d0[n-1]+dn1[n-2]+dn2[0]);
    if(res == LINF) res = -1;
    cout << res << endl;
	return 0;
}
0