結果

問題 No.1283 Extra Fee
ユーザー tentententen
提出日時 2020-11-09 19:58:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,813 ms / 2,000 ms
コード長 2,638 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 78,588 KB
実行使用メモリ 98,364 KB
最終ジャッジ日時 2024-04-27 23:41:06
合計ジャッジ時間 29,641 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
52,588 KB
testcase_01 AC 107 ms
53,628 KB
testcase_02 AC 124 ms
53,992 KB
testcase_03 AC 109 ms
54,008 KB
testcase_04 AC 114 ms
53,976 KB
testcase_05 AC 113 ms
53,956 KB
testcase_06 AC 122 ms
54,424 KB
testcase_07 AC 115 ms
53,992 KB
testcase_08 AC 154 ms
54,336 KB
testcase_09 AC 129 ms
54,160 KB
testcase_10 AC 121 ms
54,000 KB
testcase_11 AC 338 ms
59,124 KB
testcase_12 AC 393 ms
59,200 KB
testcase_13 AC 404 ms
59,216 KB
testcase_14 AC 849 ms
69,276 KB
testcase_15 AC 1,022 ms
71,692 KB
testcase_16 AC 475 ms
59,300 KB
testcase_17 AC 1,710 ms
93,764 KB
testcase_18 AC 1,632 ms
74,044 KB
testcase_19 AC 1,813 ms
74,076 KB
testcase_20 AC 1,696 ms
74,100 KB
testcase_21 AC 1,613 ms
73,796 KB
testcase_22 AC 1,528 ms
73,988 KB
testcase_23 AC 1,369 ms
73,872 KB
testcase_24 AC 1,450 ms
73,916 KB
testcase_25 AC 1,799 ms
73,900 KB
testcase_26 AC 1,752 ms
74,876 KB
testcase_27 AC 1,691 ms
73,672 KB
testcase_28 AC 1,724 ms
73,896 KB
testcase_29 AC 1,781 ms
98,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] fees = new int[n * n];
        for (int i = 0; i < m; i++) {
            int idx = (sc.nextInt() - 1) * n + sc.nextInt() - 1;
            fees[idx] = sc.nextInt();
        }
        long[][] costs = new long[2][n * n];
        Arrays.fill(costs[0], Long.MAX_VALUE);
        Arrays.fill(costs[1], Long.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.type][p.idx] <= p.value + fees[p.idx]) {
                continue;
            }
            costs[p.type][p.idx] = p.value + fees[p.idx];
            if (p.idx / n > 0) {
                queue.add(new Path(p.idx - n, p.type, p.value + fees[p.idx] + 1));
            }
            if (p.idx / n < n - 1) {
                queue.add(new Path(p.idx + n, p.type, p.value + fees[p.idx] + 1));
            }
            if (p.idx % n > 0) {
                queue.add(new Path(p.idx - 1, p.type, p.value + fees[p.idx] + 1));
            }
            if (p.idx % n < n - 1) {
                queue.add(new Path(p.idx + 1, p.type, p.value + fees[p.idx] + 1));
            }
            if (p.type == 1) {
                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 - n, 1, p.value + 1));
            }
            if (p.idx / n < n - 1) {
                queue.add(new Path(p.idx + n, 1, p.value + 1));
            }
            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));
            }
        }
        System.out.println(costs[1][n * n - 1]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int type;
        long value;
        
        public Path(int idx, int type, long value) {
            this.idx = idx;
            this.type = type;
            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