結果

問題 No.2565 はじめてのおつかい
ユーザー shumibakoshumibako
提出日時 2023-12-02 15:25:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 1,817 bytes
コンパイル時間 5,628 ms
コンパイル使用メモリ 241,288 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2023-12-02 15:25:43
合計ジャッジ時間 9,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 207 ms
14,976 KB
testcase_04 AC 126 ms
14,976 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 80 ms
6,548 KB
testcase_07 AC 38 ms
6,548 KB
testcase_08 AC 40 ms
6,548 KB
testcase_09 AC 68 ms
6,548 KB
testcase_10 AC 51 ms
6,548 KB
testcase_11 AC 129 ms
9,700 KB
testcase_12 AC 23 ms
6,548 KB
testcase_13 AC 50 ms
6,548 KB
testcase_14 AC 95 ms
7,680 KB
testcase_15 AC 95 ms
7,168 KB
testcase_16 AC 76 ms
9,656 KB
testcase_17 AC 16 ms
6,548 KB
testcase_18 AC 21 ms
6,548 KB
testcase_19 AC 116 ms
8,960 KB
testcase_20 AC 105 ms
8,448 KB
testcase_21 AC 86 ms
8,516 KB
testcase_22 AC 48 ms
6,784 KB
testcase_23 AC 59 ms
6,912 KB
testcase_24 AC 10 ms
6,548 KB
testcase_25 AC 115 ms
8,576 KB
testcase_26 AC 60 ms
6,548 KB
testcase_27 AC 82 ms
8,752 KB
testcase_28 AC 112 ms
8,704 KB
testcase_29 AC 135 ms
10,052 KB
testcase_30 AC 97 ms
9,472 KB
testcase_31 AC 104 ms
8,704 KB
testcase_32 AC 56 ms
6,656 KB
testcase_33 AC 103 ms
9,216 KB
testcase_34 AC 96 ms
8,040 KB
testcase_35 AC 89 ms
10,224 KB
testcase_36 AC 125 ms
9,200 KB
testcase_37 AC 55 ms
6,784 KB
testcase_38 AC 109 ms
8,064 KB
testcase_39 AC 81 ms
7,168 KB
testcase_40 AC 114 ms
10,104 KB
testcase_41 AC 118 ms
10,640 KB
testcase_42 AC 56 ms
6,548 KB
testcase_43 AC 95 ms
7,668 KB
testcase_44 AC 48 ms
6,548 KB
testcase_45 AC 22 ms
6,548 KB
testcase_46 AC 93 ms
7,296 KB
testcase_47 AC 59 ms
6,548 KB
testcase_48 AC 47 ms
6,548 KB
testcase_49 AC 24 ms
6,548 KB
testcase_50 AC 66 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 45 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cctype>
#include <string>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#include <atcoder/all>
#include <math.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using namespace atcoder;
using ll = long long;
using p = pair<int,int>;
using mod = modint998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

vector<pair<char,int>> RunLength(string s){
    vector<pair<char,int>> R;
    int n = s.size();
    int pre = 0;
    rep(i,n - 1){
        if(s[i + 1] != s[i]){
            R.push_back({s[i],i - pre + 1});
            pre = i + 1;
        }
    }
    if(pre != n)R.push_back({s[n - 1],n - pre});
    return R;
}

int op(int a,int b){return min(a,b);}
int e(){return (int)1e9;}
segtree<int,op,e> seg(1);

int n,m;
map<int,vector<int>> mp;
int bfs(int start,int goal){
    queue<int> Q;
    vector<int> seen(n,-1);
    Q.push(start);
    seen[start] = 0;
    while(Q.size()){
        int now = Q.front();Q.pop();
        for(int next:mp[now]){
            if(seen[next] != -1)continue;
            seen[next] = seen[now] + 1;
            Q.push(next);
            if(next == goal)Q = {};
        }
    }
    return seen[goal];
}

int main(){
    cin >> n >> m;
    rep(i,m){
        int u,v;cin >> u >> v;
        u--;v--;
        mp[u].push_back(v);
    }

    int ans = -1;
    //1 N N - 1 1
    int a = bfs(0,n - 2);
    int b = bfs(n - 2,n - 1);
    int c = bfs(n - 1,0);
    if(a != -1 && b != -1 && c != -1)ans = a + b + c;
    a = bfs(0,n - 1);
    b = bfs(n - 1,n - 2);
    c = bfs(n - 2,0);
    if(a != -1 && b != -1 && c != -1)ans = min(ans,a + b + c);
    cout << ans << endl;
    
}
0