結果

問題 No.2565 はじめてのおつかい
ユーザー nononnonon
提出日時 2023-12-02 18:43:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 813 bytes
コンパイル時間 2,362 ms
コンパイル使用メモリ 207,532 KB
実行使用メモリ 10,132 KB
最終ジャッジ日時 2023-12-02 18:43:20
合計ジャッジ時間 5,944 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,676 KB
testcase_01 AC 4 ms
6,676 KB
testcase_02 AC 4 ms
6,676 KB
testcase_03 AC 74 ms
10,132 KB
testcase_04 AC 34 ms
10,132 KB
testcase_05 AC 3 ms
6,676 KB
testcase_06 AC 29 ms
7,516 KB
testcase_07 AC 16 ms
7,132 KB
testcase_08 AC 16 ms
7,132 KB
testcase_09 AC 26 ms
7,388 KB
testcase_10 AC 20 ms
7,260 KB
testcase_11 AC 43 ms
8,284 KB
testcase_12 AC 12 ms
6,876 KB
testcase_13 AC 19 ms
7,132 KB
testcase_14 AC 32 ms
7,772 KB
testcase_15 AC 32 ms
7,644 KB
testcase_16 AC 29 ms
8,524 KB
testcase_17 AC 9 ms
6,876 KB
testcase_18 AC 10 ms
7,260 KB
testcase_19 AC 37 ms
8,124 KB
testcase_20 AC 33 ms
7,988 KB
testcase_21 AC 46 ms
8,004 KB
testcase_22 AC 18 ms
7,516 KB
testcase_23 AC 21 ms
7,516 KB
testcase_24 AC 7 ms
6,748 KB
testcase_25 AC 34 ms
8,012 KB
testcase_26 AC 21 ms
7,388 KB
testcase_27 AC 30 ms
8,072 KB
testcase_28 AC 36 ms
8,028 KB
testcase_29 AC 40 ms
8,548 KB
testcase_30 AC 32 ms
8,208 KB
testcase_31 AC 34 ms
8,028 KB
testcase_32 AC 19 ms
7,388 KB
testcase_33 AC 32 ms
8,128 KB
testcase_34 AC 35 ms
7,900 KB
testcase_35 AC 33 ms
8,468 KB
testcase_36 AC 34 ms
8,128 KB
testcase_37 AC 20 ms
7,516 KB
testcase_38 AC 33 ms
7,900 KB
testcase_39 AC 29 ms
7,644 KB
testcase_40 AC 38 ms
8,516 KB
testcase_41 AC 41 ms
8,620 KB
testcase_42 AC 22 ms
7,388 KB
testcase_43 AC 30 ms
7,772 KB
testcase_44 AC 17 ms
7,132 KB
testcase_45 AC 10 ms
7,004 KB
testcase_46 AC 34 ms
7,772 KB
testcase_47 AC 22 ms
7,260 KB
testcase_48 AC 20 ms
7,260 KB
testcase_49 AC 11 ms
6,876 KB
testcase_50 AC 23 ms
7,260 KB
testcase_51 AC 4 ms
6,676 KB
testcase_52 AC 19 ms
7,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0