結果

問題 No.1868 Teleporting Cyanmond
ユーザー tentententen
提出日時 2022-08-09 10:51:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 1,887 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 78,236 KB
実行使用メモリ 61,924 KB
最終ジャッジ日時 2023-10-19 21:31:03
合計ジャッジ時間 10,660 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,680 KB
testcase_01 AC 57 ms
52,604 KB
testcase_02 AC 56 ms
53,676 KB
testcase_03 AC 298 ms
61,924 KB
testcase_04 AC 255 ms
59,956 KB
testcase_05 AC 108 ms
55,592 KB
testcase_06 AC 148 ms
57,644 KB
testcase_07 AC 229 ms
59,272 KB
testcase_08 AC 175 ms
58,064 KB
testcase_09 AC 210 ms
58,972 KB
testcase_10 AC 268 ms
59,284 KB
testcase_11 AC 85 ms
54,808 KB
testcase_12 AC 171 ms
58,280 KB
testcase_13 AC 203 ms
59,096 KB
testcase_14 AC 266 ms
59,520 KB
testcase_15 AC 246 ms
59,144 KB
testcase_16 AC 130 ms
55,732 KB
testcase_17 AC 155 ms
56,088 KB
testcase_18 AC 305 ms
59,768 KB
testcase_19 AC 295 ms
59,580 KB
testcase_20 AC 278 ms
59,756 KB
testcase_21 AC 278 ms
59,836 KB
testcase_22 AC 294 ms
59,920 KB
testcase_23 AC 294 ms
59,464 KB
testcase_24 AC 294 ms
59,752 KB
testcase_25 AC 291 ms
59,564 KB
testcase_26 AC 293 ms
59,232 KB
testcase_27 AC 294 ms
59,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n + 1];
        for (int i = 1; i < n; i++) {
            values[i] = sc.nextInt();
        }
        int[] costs = new int[n + 1];
        Arrays.fill(costs, Integer.MAX_VALUE);
        costs[0] = 0;
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(1, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            queue.add(new Path(values[p.idx], p.value + 1));
            queue.add(new Path(p.idx - 1, p.value));
        }
        System.out.println(costs[n]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0