結果

問題 No.1283 Extra Fee
ユーザー tentententen
提出日時 2021-03-19 14:40:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,487 ms / 2,000 ms
コード長 2,905 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 78,960 KB
実行使用メモリ 93,600 KB
最終ジャッジ日時 2024-04-27 23:49:40
合計ジャッジ時間 23,154 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
48,356 KB
testcase_01 AC 41 ms
36,984 KB
testcase_02 AC 43 ms
36,716 KB
testcase_03 AC 42 ms
37,012 KB
testcase_04 AC 42 ms
36,844 KB
testcase_05 AC 42 ms
36,832 KB
testcase_06 AC 44 ms
36,988 KB
testcase_07 AC 41 ms
36,564 KB
testcase_08 AC 45 ms
36,700 KB
testcase_09 AC 45 ms
37,168 KB
testcase_10 AC 45 ms
36,960 KB
testcase_11 AC 217 ms
46,064 KB
testcase_12 AC 239 ms
46,992 KB
testcase_13 AC 206 ms
45,232 KB
testcase_14 AC 420 ms
53,324 KB
testcase_15 AC 728 ms
58,964 KB
testcase_16 AC 226 ms
45,892 KB
testcase_17 AC 1,257 ms
86,472 KB
testcase_18 AC 1,296 ms
88,564 KB
testcase_19 AC 1,394 ms
89,152 KB
testcase_20 AC 1,377 ms
87,616 KB
testcase_21 AC 1,337 ms
88,344 KB
testcase_22 AC 1,233 ms
85,068 KB
testcase_23 AC 1,079 ms
82,408 KB
testcase_24 AC 1,096 ms
85,660 KB
testcase_25 AC 1,487 ms
90,684 KB
testcase_26 AC 1,442 ms
90,356 KB
testcase_27 AC 1,400 ms
90,156 KB
testcase_28 AC 1,431 ms
90,508 KB
testcase_29 AC 1,437 ms
93,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        HashMap<Integer, Integer> places = new HashMap<>();
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            places.put((Integer.parseInt(line[0]) - 1) * n + Integer.parseInt(line[1]) - 1, Integer.parseInt(line[2]));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, 0));
        long[][] costs = new long[2][n * n];
        Arrays.fill(costs[0], Long.MAX_VALUE);
        Arrays.fill(costs[1], Long.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            int add = places.getOrDefault(p.idx, 0);
            if (costs[p.level][p.idx] <= p.value + add) {
                continue;
            }
            costs[p.level][p.idx] = p.value + add;
            if (p.idx % n > 0) {
                queue.add(new Path(p.idx - 1, p.level, p.value + 1 + add));
            }
            if (p.idx % n < n - 1) {
                queue.add(new Path(p.idx + 1, p.level, p.value + 1 + add));
            }
            if (p.idx / n > 0) {
                queue.add(new Path(p.idx - n, p.level, p.value + 1 + add));
            }
            if (p.idx / n < n - 1) {
                queue.add(new Path(p.idx + n, p.level, p.value + 1 + add));
            }
            if (p.level > 0) {
                continue;
            }
            if (costs[1][p.idx] <= p.value) {
                continue;
            }
            costs[1][p.idx] = p.value;
            if (p.idx % n > 0) {
                queue.add(new Path(p.idx - 1, 1, p.value + 1));
            }
            if (p.idx % n < n - 1) {
                queue.add(new Path(p.idx + 1, 1, p.value + 1));
            }
            if (p.idx / n > 0) {
                queue.add(new Path(p.idx - n, 1, p.value + 1));
            }
            if (p.idx / n < n - 1) {
                queue.add(new Path(p.idx + n, 1, p.value + 1));
            }
        }
        System.out.println(costs[1][n * n - 1]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int level;
        long value;
        
        public Path(int idx, int level, long value) {
            this.idx = idx;
            this.level = level;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
 }
0