結果

問題 No.2565 はじめてのおつかい
ユーザー shihakashihaka
提出日時 2023-12-02 16:42:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 213,900 KB
実行使用メモリ 14,248 KB
最終ジャッジ日時 2023-12-02 16:42:29
合計ジャッジ時間 7,278 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,640 KB
testcase_01 AC 5 ms
9,640 KB
testcase_02 AC 5 ms
9,640 KB
testcase_03 AC 85 ms
14,248 KB
testcase_04 AC 67 ms
14,248 KB
testcase_05 AC 5 ms
9,640 KB
testcase_06 AC 58 ms
13,096 KB
testcase_07 AC 33 ms
11,560 KB
testcase_08 AC 33 ms
11,176 KB
testcase_09 AC 54 ms
12,968 KB
testcase_10 AC 39 ms
11,560 KB
testcase_11 AC 80 ms
13,736 KB
testcase_12 AC 22 ms
11,176 KB
testcase_13 AC 38 ms
11,688 KB
testcase_14 AC 60 ms
12,712 KB
testcase_15 AC 73 ms
12,968 KB
testcase_16 AC 56 ms
12,968 KB
testcase_17 AC 16 ms
10,408 KB
testcase_18 AC 19 ms
10,536 KB
testcase_19 AC 74 ms
13,608 KB
testcase_20 AC 63 ms
13,096 KB
testcase_21 AC 57 ms
13,096 KB
testcase_22 AC 35 ms
11,560 KB
testcase_23 AC 42 ms
11,816 KB
testcase_24 AC 10 ms
10,024 KB
testcase_25 AC 66 ms
12,968 KB
testcase_26 AC 41 ms
11,816 KB
testcase_27 AC 57 ms
13,096 KB
testcase_28 AC 71 ms
13,608 KB
testcase_29 AC 76 ms
13,864 KB
testcase_30 AC 61 ms
13,224 KB
testcase_31 AC 66 ms
13,224 KB
testcase_32 AC 39 ms
11,688 KB
testcase_33 AC 63 ms
13,096 KB
testcase_34 AC 71 ms
13,224 KB
testcase_35 AC 69 ms
13,224 KB
testcase_36 AC 70 ms
13,096 KB
testcase_37 AC 43 ms
11,816 KB
testcase_38 AC 71 ms
13,352 KB
testcase_39 AC 62 ms
12,712 KB
testcase_40 AC 72 ms
13,864 KB
testcase_41 AC 76 ms
13,864 KB
testcase_42 AC 43 ms
11,944 KB
testcase_43 AC 65 ms
12,968 KB
testcase_44 AC 36 ms
11,304 KB
testcase_45 AC 20 ms
10,536 KB
testcase_46 AC 76 ms
13,352 KB
testcase_47 AC 46 ms
12,712 KB
testcase_48 AC 39 ms
11,816 KB
testcase_49 AC 21 ms
11,048 KB
testcase_50 AC 54 ms
13,352 KB
testcase_51 AC 5 ms
9,640 KB
testcase_52 AC 44 ms
13,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
template<typename T> inline bool chmax(T &a, T b) {return ((a < b) ? (a = b, true) : (false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a > b) ? (a = b, true) : (false));}
typedef long long ll;
typedef pair<ll,ll> P;

struct edge { 
    ll to, cost; 
    edge(ll to, ll cost): to(to), cost(cost) {}
};
ll MAX_V = 200005, INF = 1001001001001001;
vector<vector<edge>> G(MAX_V);
vector<ll> d(MAX_V,INF);

void dijkstra(int s) {
    priority_queue<P,vector<P>,greater<P>> que;
    d[s] = 0;
    que.emplace(0, s);
    
    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first)
            continue;
        rep(i,G[v].size()) {
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.emplace(d[e.to], e.to);
            }
        }
    }
}

int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> u(m), v(m);
    rep(i,m) cin >> u[i] >> v[i];

    rep(i,m) {
        G[u[i]].emplace_back(v[i],1);
    }

    ll sum1 = 0, sum2 = 0;
    dijkstra(1);
    sum1 += d[n-1];
    sum2 += d[n];
    rep(i,MAX_V) {
        d[i] = INF;
    }
    //cout << " " << sum1 << " " << sum2 << endl;
    dijkstra(n-1);
    sum1 += d[n];
    sum2 += d[1];
    rep(i,MAX_V) {
        d[i] = INF;
    }
    //cout << " " << sum1 << " " << sum2 << endl;
    dijkstra(n);
    sum1 += d[1];
    sum2 += d[n-1];

    ll ans = min(sum1, sum2);
    if(ans >= INF)
        ans = -1;
    cout << ans << endl;







    return 0;
}
0