結果

問題 No.2565 はじめてのおつかい
ユーザー umezoumezo
提出日時 2023-12-02 15:58:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 213,984 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2023-12-02 15:58:27
合計ジャッジ時間 5,681 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 56 ms
11,008 KB
testcase_04 AC 34 ms
11,008 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 35 ms
6,548 KB
testcase_07 AC 18 ms
6,548 KB
testcase_08 AC 20 ms
6,548 KB
testcase_09 AC 30 ms
6,548 KB
testcase_10 AC 24 ms
6,548 KB
testcase_11 AC 59 ms
8,832 KB
testcase_12 AC 10 ms
6,548 KB
testcase_13 AC 23 ms
6,548 KB
testcase_14 AC 40 ms
7,168 KB
testcase_15 AC 42 ms
6,912 KB
testcase_16 AC 31 ms
9,600 KB
testcase_17 AC 7 ms
6,548 KB
testcase_18 AC 9 ms
6,548 KB
testcase_19 AC 48 ms
8,064 KB
testcase_20 AC 41 ms
7,808 KB
testcase_21 AC 34 ms
7,680 KB
testcase_22 AC 19 ms
6,548 KB
testcase_23 AC 25 ms
6,548 KB
testcase_24 AC 5 ms
6,548 KB
testcase_25 AC 45 ms
7,680 KB
testcase_26 AC 26 ms
6,548 KB
testcase_27 AC 33 ms
7,936 KB
testcase_28 AC 46 ms
7,936 KB
testcase_29 AC 52 ms
9,216 KB
testcase_30 AC 37 ms
8,448 KB
testcase_31 AC 42 ms
7,936 KB
testcase_32 AC 24 ms
6,548 KB
testcase_33 AC 42 ms
8,192 KB
testcase_34 AC 48 ms
7,552 KB
testcase_35 AC 36 ms
8,960 KB
testcase_36 AC 48 ms
8,192 KB
testcase_37 AC 24 ms
6,548 KB
testcase_38 AC 43 ms
7,552 KB
testcase_39 AC 38 ms
6,784 KB
testcase_40 AC 45 ms
9,088 KB
testcase_41 AC 53 ms
9,472 KB
testcase_42 AC 26 ms
6,548 KB
testcase_43 AC 38 ms
7,296 KB
testcase_44 AC 21 ms
6,548 KB
testcase_45 AC 10 ms
6,548 KB
testcase_46 AC 45 ms
7,168 KB
testcase_47 AC 24 ms
6,548 KB
testcase_48 AC 24 ms
6,548 KB
testcase_49 AC 10 ms
6,548 KB
testcase_50 AC 26 ms
6,548 KB
testcase_51 AC 2 ms
6,548 KB
testcase_52 AC 18 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

Graph G;
vector<ll> dijkstra(int s){
  int n=G.size();
  vector<ll> dist(n,INF);
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;

    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  return dist;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n,m;
  cin>>n>>m;
  
  G.resize(n);
  rep(i,m){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(Edge(b,1));
  }
  
  vector<ll> dis=dijkstra(0);
  vector<ll> dis1=dijkstra(n-2);
  vector<ll> dis2=dijkstra(n-1);
  
  ll mi=INF;
  mi=min(mi,dis[n-2]+dis1[n-1]+dis2[0]);
  mi=min(mi,dis[n-1]+dis2[n-2]+dis1[0]);
  if(mi==INF) cout<<-1<<endl;
  else cout<<mi<<endl;
  
  return 0;
}
0