結果
| 問題 | No.2565 はじめてのおつかい | 
| コンテスト | |
| ユーザー |  nonon | 
| 提出日時 | 2023-12-02 18:43:13 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 59 ms / 2,000 ms | 
| コード長 | 813 bytes | 
| コンパイル時間 | 1,998 ms | 
| コンパイル使用メモリ | 200,040 KB | 
| 最終ジャッジ日時 | 2025-02-18 06:10:21 | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 50 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
int N,M;
vector<int>G[1<<17];
int dist(int i, int j)
{
    i--,j--;
    vector<int>dist(N,1<<25);
    dist[i]=0;
    queue<int>q;
    q.push(i);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int v:G[u])
        {
            if(dist[v]>dist[u]+1)
            {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
    return dist[j];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>N>>M;
    for(;M--;)
    {
        int u,v;
        cin>>u>>v;
        u--,v--;
        G[u].push_back(v);
    }
    int ans1=dist(1,N)+dist(N,N-1)+dist(N-1,1);
    int ans2=dist(1,N-1)+dist(N-1,N)+dist(N,1);
    int ans=min(ans1,ans2);
    cout << (ans>(1<<25)?-1:ans) << endl;
}
            
            
            
        