結果

問題 No.2565 はじめてのおつかい
ユーザー yumekawayuiyumekawayui
提出日時 2023-12-02 15:34:45
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,990 bytes
コンパイル時間 4,217 ms
コンパイル使用メモリ 189,300 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2023-12-02 15:34:54
合計ジャッジ時間 7,783 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 47 ms
13,440 KB
testcase_04 AC 32 ms
13,440 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 42 ms
6,676 KB
testcase_07 AC 22 ms
6,676 KB
testcase_08 AC 26 ms
6,676 KB
testcase_09 AC 36 ms
6,676 KB
testcase_10 AC 28 ms
6,676 KB
testcase_11 AC 64 ms
9,472 KB
testcase_12 AC 10 ms
6,676 KB
testcase_13 AC 28 ms
6,676 KB
testcase_14 AC 47 ms
7,424 KB
testcase_15 AC 48 ms
7,040 KB
testcase_16 AC 24 ms
11,392 KB
testcase_17 AC 5 ms
6,676 KB
testcase_18 AC 8 ms
6,676 KB
testcase_19 AC 50 ms
8,704 KB
testcase_20 AC 44 ms
8,192 KB
testcase_21 AC 33 ms
8,192 KB
testcase_22 AC 18 ms
6,676 KB
testcase_23 AC 27 ms
6,676 KB
testcase_24 AC 4 ms
6,676 KB
testcase_25 AC 66 ms
8,320 KB
testcase_26 AC 29 ms
6,676 KB
testcase_27 AC 32 ms
8,704 KB
testcase_28 AC 49 ms
8,448 KB
testcase_29 AC 54 ms
10,112 KB
testcase_30 AC 33 ms
9,344 KB
testcase_31 AC 45 ms
8,448 KB
testcase_32 AC 27 ms
6,676 KB
testcase_33 AC 42 ms
8,960 KB
testcase_34 AC 56 ms
7,808 KB
testcase_35 AC 30 ms
10,240 KB
testcase_36 AC 51 ms
8,960 KB
testcase_37 AC 28 ms
6,676 KB
testcase_38 AC 47 ms
7,808 KB
testcase_39 AC 45 ms
7,040 KB
testcase_40 AC 40 ms
9,984 KB
testcase_41 AC 50 ms
10,624 KB
testcase_42 AC 29 ms
6,676 KB
testcase_43 AC 41 ms
7,424 KB
testcase_44 AC 25 ms
6,676 KB
testcase_45 AC 11 ms
6,676 KB
testcase_46 AC 54 ms
7,168 KB
testcase_47 AC 30 ms
6,676 KB
testcase_48 AC 27 ms
6,676 KB
testcase_49 AC 9 ms
6,676 KB
testcase_50 AC 26 ms
6,676 KB
testcase_51 AC 1 ms
6,676 KB
testcase_52 AC 16 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
    

}
0