結果
問題 |
No.2565 はじめてのおつかい
|
ユーザー |
![]() |
提出日時 | 2023-12-02 15:34:45 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 67 ms / 2,000 ms |
コード長 | 1,990 bytes |
コンパイル時間 | 4,674 ms |
コンパイル使用メモリ | 193,536 KB |
実行使用メモリ | 13,312 KB |
最終ジャッジ日時 | 2024-09-26 18:55:21 |
合計ジャッジ時間 | 7,868 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/extc++.h> using namespace std; using ld = long double; const vector<int> dx = {0, 0, 1, -1}; const vector<int> dy = {1, -1, 0, 0}; #define vec vector #define int long long #define double long double //cout<<setprecision(10)<<fixed<<.. #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repp(i, x, n) for (int i = x; i < (int)(n); i++) #define pii pair<int,int> #define pq priority_queue #define all(V) begin(V),end(V) #define printpair(p) cout<<p.first<<" "<<p.second<<"\n" #define tple tuple<int,int,int> template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } #define mend(a,b) G[a].push_back(b) vec<int> daiku(vec<vec<int>> &G,int s,int n){ pq<pii,vec<pii>,greater<pii>> Q; vec<bool> done(n+1,false); vec<int> dist(n+1,1e9); Q.push({0,s}); dist[s]=0; while(Q.size()){ int v=Q.top().second; Q.pop(); if(done[v]){continue;} done[v]=true; for(int nv:G[v]){ if(dist[nv]>dist[v]+1){ dist[nv]=dist[v]+1; Q.push({dist[nv],nv}); } } } return dist; } signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m;cin>>n>>m; vec G(n+1,vec<int>()); rep(i,m){ int a,b;cin>>a>>b; G[a].push_back(b); } vec<int> one_to_n1=daiku(G,1,n); vec<int> n1_to_n=daiku(G,n-1,n); vec<int> n_to_1=daiku(G,n,n); int inf=1e9; int ans=one_to_n1[n-1]+n1_to_n[n]+n_to_1[1]; vec<int> one_to_n=daiku(G,1,n); vec<int> n_to_n1=daiku(G,n,n); vec<int> n1_to_1=daiku(G,n-1,n); chmin(ans,one_to_n[n]+n_to_n1[n-1]+n1_to_1[1]); if(ans>=inf){ cout<<-1<<"\n";return 0; } cout<<ans<<"\n"; }