結果

問題 No.2565 はじめてのおつかい
ユーザー aki
提出日時 2023-11-23 14:39:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 918 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 203,164 KB
最終ジャッジ日時 2025-02-17 23:19:33
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;



int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n,m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
    }

    auto bfs = [&](int s, int t) -> ll {
        vector<ll> dist(n,1e9);
        dist[s] = 0;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int p = q.front();
            q.pop();
            for(int x:g[p]){
                if(dist[x]!=1e9) continue;
                dist[x] = dist[p]+1;
                q.push(x);
            }
        }
        return dist[t];
    };
    ll ans = min(bfs(0,n-2)+bfs(n-2,n-1)+bfs(n-1,0),bfs(0,n-1)+bfs(n-1,n-2)+bfs(n-2,0));

    if(ans>=1e9) cout << -1 << endl;
    else cout << ans << endl;
    
}
0