結果

問題 No.2565 はじめてのおつかい
ユーザー twooimp2twooimp2
提出日時 2023-12-02 16:06:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,116 bytes
コンパイル時間 2,306 ms
コンパイル使用メモリ 211,916 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2023-12-02 16:07:05
合計ジャッジ時間 6,514 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 87 ms
11,136 KB
testcase_04 AC 69 ms
11,136 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 53 ms
6,548 KB
testcase_07 AC 29 ms
6,548 KB
testcase_08 AC 27 ms
6,548 KB
testcase_09 AC 50 ms
6,548 KB
testcase_10 AC 33 ms
6,548 KB
testcase_11 AC 76 ms
8,064 KB
testcase_12 AC 20 ms
6,548 KB
testcase_13 AC 33 ms
6,548 KB
testcase_14 AC 54 ms
6,548 KB
testcase_15 AC 57 ms
6,548 KB
testcase_16 AC 60 ms
9,344 KB
testcase_17 AC 13 ms
6,548 KB
testcase_18 AC 17 ms
6,548 KB
testcase_19 AC 68 ms
7,424 KB
testcase_20 AC 60 ms
7,040 KB
testcase_21 AC 59 ms
7,040 KB
testcase_22 AC 34 ms
6,548 KB
testcase_23 AC 39 ms
6,548 KB
testcase_24 AC 7 ms
6,548 KB
testcase_25 AC 60 ms
7,168 KB
testcase_26 AC 36 ms
6,548 KB
testcase_27 AC 58 ms
7,424 KB
testcase_28 AC 66 ms
7,296 KB
testcase_29 AC 75 ms
8,448 KB
testcase_30 AC 64 ms
7,808 KB
testcase_31 AC 62 ms
7,168 KB
testcase_32 AC 36 ms
6,548 KB
testcase_33 AC 60 ms
7,680 KB
testcase_34 AC 62 ms
6,784 KB
testcase_35 AC 65 ms
8,576 KB
testcase_36 AC 62 ms
7,552 KB
testcase_37 AC 36 ms
6,548 KB
testcase_38 AC 62 ms
6,784 KB
testcase_39 AC 49 ms
6,548 KB
testcase_40 AC 75 ms
8,320 KB
testcase_41 AC 76 ms
8,832 KB
testcase_42 AC 38 ms
6,548 KB
testcase_43 AC 55 ms
6,548 KB
testcase_44 AC 28 ms
6,548 KB
testcase_45 AC 16 ms
6,548 KB
testcase_46 AC 61 ms
6,548 KB
testcase_47 AC 43 ms
6,548 KB
testcase_48 AC 33 ms
6,548 KB
testcase_49 AC 19 ms
6,548 KB
testcase_50 AC 52 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 42 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
  ll n,m;
  cin>>n>>m;
  vector<vector<ll>> G(n);
  for(ll i=0;i<m;i++){
    ll u,v;
    cin>>u>>v;
    u--;
    v--;
    G[u].push_back(v);
  }
  queue<ll> que;
  vector<ll> dist1(n,1e18);
  que.push(0);
  dist1[0]=0;
  while(que.size()){
    ll q=que.front();
    que.pop();
    for(ll u:G[q]){
      if(dist1[u]==1e18){
        dist1[u]=dist1[q]+1;
        que.push(u);
      }
    }
  }
  vector<ll> dist2(n,1e18);
  que.push(n-2);
  dist2[n-2]=0;
  while(que.size()){
    ll q=que.front();
    que.pop();
    for(ll u:G[q]){
      if(dist2[u]==1e18){
        dist2[u]=dist2[q]+1;
        que.push(u);
      }
    }
  }
  vector<ll> dist3(n,1e18);
  que.push(n-1);
  dist3[n-1]=0;
  while(que.size()){
    ll q=que.front();
    que.pop();
    for(ll u:G[q]){
      if(dist3[u]==1e18){
        dist3[u]=dist3[q]+1;
        que.push(u);
      }
    }
  }
  ll ans=1e18;
  ans=min(ans,dist1[n-2]+dist2[n-1]+dist3[0]);
  ans=min(ans,dist1[n-1]+dist3[n-2]+dist2[0]);
  if(ans==1e18){
    cout<<-1<<endl;
  }else{
    cout<<ans<<endl;
  }
}
0