結果

問題 No.1868 Teleporting Cyanmond
ユーザー rogi52rogi52
提出日時 2022-03-11 21:22:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 918 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 207,188 KB
実行使用メモリ 9,064 KB
最終ジャッジ日時 2023-10-14 06:01:35
合計ジャッジ時間 3,799 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 29 ms
8,264 KB
testcase_04 AC 19 ms
6,684 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 6 ms
4,348 KB
testcase_07 AC 17 ms
5,820 KB
testcase_08 AC 11 ms
4,564 KB
testcase_09 AC 14 ms
5,528 KB
testcase_10 AC 30 ms
8,112 KB
testcase_11 AC 2 ms
4,480 KB
testcase_12 AC 9 ms
4,500 KB
testcase_13 AC 16 ms
5,732 KB
testcase_14 AC 31 ms
8,256 KB
testcase_15 AC 21 ms
6,768 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 8 ms
4,348 KB
testcase_18 AC 28 ms
8,832 KB
testcase_19 AC 27 ms
9,064 KB
testcase_20 AC 27 ms
8,800 KB
testcase_21 AC 27 ms
8,912 KB
testcase_22 AC 27 ms
8,784 KB
testcase_23 AC 25 ms
8,784 KB
testcase_24 AC 25 ms
8,820 KB
testcase_25 AC 25 ms
8,800 KB
testcase_26 AC 25 ms
8,880 KB
testcase_27 AC 24 ms
8,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

template < class T >
vector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {
    T INF = numeric_limits< T >::max();
    vector<T> dist(graph.size(), INF);
    priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
    q.push({dist[s] = T(0), s});
    while(!q.empty()){
        auto [uc, ui] = q.top(); q.pop();
        if(uc != dist[ui]) continue;
        for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) 
            q.push({dist[vi] = uc + vc, vi});
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<vector<pair<int,int>>> G(N);
    rep(i,N-1) {
        int R; cin >> R; R--;
        G[i].push_back({R, 1});
        G[i + 1].push_back({i, 0});
    }
    cout << dijkstra(G, 0)[N - 1] << endl;
}
0