結果
| 問題 | No.2565 はじめてのおつかい | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-02 15:25:31 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 276 ms / 2,000 ms | 
| コード長 | 1,817 bytes | 
| コンパイル時間 | 4,430 ms | 
| コンパイル使用メモリ | 241,340 KB | 
| 実行使用メモリ | 14,980 KB | 
| 最終ジャッジ日時 | 2024-09-26 18:40:15 | 
| 合計ジャッジ時間 | 10,281 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 50 | 
ソースコード
#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;
    
}
            
            
            
        