結果

問題 No.2565 はじめてのおつかい
ユーザー cumene_rxcumene_rx
提出日時 2023-12-02 16:55:46
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,924 bytes
コンパイル時間 6,261 ms
コンパイル使用メモリ 321,524 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2023-12-02 16:55:58
合計ジャッジ時間 11,581 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 99 ms
10,752 KB
testcase_04 AC 70 ms
10,752 KB
testcase_05 WA -
testcase_06 AC 89 ms
6,676 KB
testcase_07 AC 46 ms
6,676 KB
testcase_08 AC 38 ms
6,676 KB
testcase_09 AC 87 ms
6,676 KB
testcase_10 AC 44 ms
6,676 KB
testcase_11 AC 101 ms
7,936 KB
testcase_12 AC 50 ms
6,676 KB
testcase_13 AC 45 ms
6,676 KB
testcase_14 AC 72 ms
6,676 KB
testcase_15 AC 81 ms
6,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 68 ms
7,040 KB
testcase_22 WA -
testcase_23 AC 47 ms
6,676 KB
testcase_24 WA -
testcase_25 AC 80 ms
7,040 KB
testcase_26 AC 48 ms
6,676 KB
testcase_27 AC 70 ms
7,296 KB
testcase_28 AC 88 ms
7,296 KB
testcase_29 AC 95 ms
8,320 KB
testcase_30 WA -
testcase_31 AC 80 ms
7,168 KB
testcase_32 AC 43 ms
6,676 KB
testcase_33 AC 79 ms
7,424 KB
testcase_34 AC 90 ms
6,784 KB
testcase_35 WA -
testcase_36 AC 82 ms
7,424 KB
testcase_37 AC 47 ms
6,676 KB
testcase_38 AC 83 ms
6,784 KB
testcase_39 AC 72 ms
6,676 KB
testcase_40 AC 91 ms
8,192 KB
testcase_41 AC 92 ms
8,576 KB
testcase_42 AC 48 ms
6,676 KB
testcase_43 AC 74 ms
6,676 KB
testcase_44 AC 38 ms
6,676 KB
testcase_45 AC 19 ms
6,676 KB
testcase_46 AC 93 ms
6,676 KB
testcase_47 AC 83 ms
6,676 KB
testcase_48 AC 47 ms
6,676 KB
testcase_49 AC 48 ms
6,676 KB
testcase_50 AC 124 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 99 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#include <random>
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
using mint9 = modint998244353;
using mint1 = modint1000000007;
//using mint = modint;
const ll INF = 1000000007;
const ll LINF = INF * INF;
//const long long LLMAX = 9223372036854775807;
const long long LLMAX = 100000000000000000;

const double TIME_1 = 1.5;
//const ll MOD = 998244353;
std::mt19937 get_rand_mt;
#define PA pair<int,int>
#define PAL pair<ll,ll>
#define TP tuple<int,int,int>
#define TPL tuple<ll, ll, ll>
#define rep(i, z, n) for(int i = z; i < n; i++)
#define repl(i, z, n) for(ll i = z; i < n; i++)
#define revrep(i, z, n) for(int i = n - 1; i >= z; i--)
#define revrepl(i, z, n) for(ll i = n - 1; i >= z; i--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define YN(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN2(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define OK cout << "OK" << endl;
#define FX(x) cout << fixed << setprecision(10) << x << endl;
#define shuffle(x) shuffle(all(x), get_rand_mt)
#define PQ priority_queue<int,vector<int>,greater<int>>
#define PQL priority_queue<ll,vector<ll>,greater<ll>>
#define PQPA priority_queue<PA,vector<PA>,greater<PA>>

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>T ceilX(T a, T b) {return (a + b - 1)/b; }
ll lmax(ll a, ll b){if(a>b) return a; return b;}
ll lmin(ll a, ll b){if(a>b) return b; return a;}
ll comb(ll X, ll Y){ll res = 1LL;ll R = X;repl(i, 1LL, lmin(Y + 1LL, X - Y + 1LL)){res *= (R - i + 1LL);res /= i;}return res;}
template<class T>void Rekkyo(vector<T> X){cout<<"Rekkyo"<<endl;for(auto Y:X){cout<<Y<<" ";}cout<<endl<<"end"<<endl;}
template<class T>void Rekkyo1(vector<T> X){cout<<"Rekkyo1"<<endl;int cnt=0;for(auto Y:X){cout<<cnt<<" "<<Y<<endl;cnt++;}cout<<"end"<<endl;}
template<class T>void Rekkyo2(vector<vector<T>> X){cout<<"Rekkyo2"<<endl;for(auto Y:X){for(auto Z:Y){cout <<Z<<" ";}cout<<endl;}cout<<"end"<<endl;}

vector<int> Eratosthenes(const int N){
    vector<bool> is_prime(N + 1);
    rep(i, 0, N + 1) is_prime[i] = true;
    vector<int> P;
    rep(i, 2, N + 1){
        if(is_prime[i]){
            for( int j = 2 * i; j <= N; j += i ) is_prime[ j ] = false;
            P.emplace_back(i);
        }
    }
    return P;
}
vector<vector<int>> make_edge(vector<int> u, vector<int> v, bool angle){
    int N = u.size();
    vector<vector<int>> res;
    rep(i, 0, N){res[u[i]].PB(v[i]);if(!angle) res[v[i]].PB(u[i]);}
    return res;
}
vector<vector<PA>> make_edge(vector<int> u, vector<int> v, vector<int> w, bool angle){
    int N = u.size();
    vector<vector<PA>> res;
    rep(i, 0, N){res[u[i]].PB(PA(v[i], w[i]));if(!angle) res[v[i]].PB(PA(u[i], w[i]));}
    return res;
}
vector<vector<PAL>> make_edgeL(vector<int> u, vector<int> v, vector<ll> w, bool angle){
    int N = u.size();
    vector<vector<PAL>> res;
    rep(i, 0, N){res[u[i]].PB(PAL(v[i], w[i]));if(!angle) res[v[i]].PB(PAL(u[i], w[i]));}
    return res;
}









int main(){
    int N, M;
    cin >> N >> M;
    vector<int> u(M), v(M);
    vector<vector<int>> edge(N);
    rep(i, 0, M){
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
        edge[u[i]].PB(v[i]);
    }
    PQPA pq;
    pq.push({0, 0});
    vector<int> from0(N, INF);
    from0[0] = 0;
    while(!pq.empty()){
        auto x = pq.top();
        pq.pop();
        int cost  = x.first;
        int now = x.second;
        chmin(from0[now], cost);
        for(auto next : edge[now]){
            if(from0[next] != INF) continue;
            else{
                pq.push({cost + 1, next});
            }
        }
    }

    pq.push({0, N - 2});
    vector<int> fromN1(N, INF);
    fromN1[N - 2] = 0;
    while(!pq.empty()){
        auto x = pq.top();
        pq.pop();
        int cost  = x.first;
        int now = x.second;
        chmin(fromN1[now], cost);
        for(auto next : edge[now]){
            if(fromN1[next] != INF) continue;
            else{
                pq.push({cost + 1, next});
            }
        }
    }
    
    pq.push({0, N - 1});
    vector<int> fromN0(N, INF);
    fromN0[N - 1] = 0;
    while(!pq.empty()){
        auto x = pq.top();
        pq.pop();
        int cost  = x.first;
        int now = x.second;
        chmin(fromN0[now], cost);
        for(auto next : edge[now]){
            if(fromN0[next] != INF) continue;
            else{
                pq.push({cost + 1, next});
            }
        }
    }
    
    int ans = INF;
    chmin(ans, from0[N - 2] + fromN1[N - 1] + fromN0[0]);
    chmin(ans, from0[N - 1] + fromN0[N - 2] + fromN1[0]);
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;
}
0