結果

問題 No.1283 Extra Fee
ユーザー tentententen
提出日時 2020-11-09 20:01:48
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,752 bytes
コンパイル時間 2,436 ms
コンパイル使用メモリ 78,228 KB
実行使用メモリ 100,244 KB
最終ジャッジ日時 2024-04-27 23:39:22
合計ジャッジ時間 29,334 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
52,756 KB
testcase_01 AC 109 ms
54,004 KB
testcase_02 AC 119 ms
53,936 KB
testcase_03 AC 110 ms
53,988 KB
testcase_04 AC 112 ms
53,704 KB
testcase_05 AC 110 ms
53,880 KB
testcase_06 AC 120 ms
54,256 KB
testcase_07 AC 110 ms
53,788 KB
testcase_08 AC 133 ms
54,192 KB
testcase_09 AC 132 ms
54,124 KB
testcase_10 AC 123 ms
54,300 KB
testcase_11 AC 343 ms
59,136 KB
testcase_12 AC 433 ms
59,244 KB
testcase_13 AC 364 ms
59,584 KB
testcase_14 AC 812 ms
69,084 KB
testcase_15 AC 1,028 ms
71,824 KB
testcase_16 AC 434 ms
59,308 KB
testcase_17 AC 1,575 ms
98,592 KB
testcase_18 AC 1,607 ms
74,092 KB
testcase_19 AC 1,705 ms
74,024 KB
testcase_20 AC 1,731 ms
73,968 KB
testcase_21 AC 1,611 ms
74,028 KB
testcase_22 AC 1,608 ms
74,068 KB
testcase_23 AC 1,406 ms
73,892 KB
testcase_24 AC 1,436 ms
74,464 KB
testcase_25 AC 1,700 ms
74,132 KB
testcase_26 TLE -
testcase_27 AC 1,674 ms
73,996 KB
testcase_28 AC 1,703 ms
73,880 KB
testcase_29 AC 1,542 ms
100,244 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));
        queue.add(new Path(0, 1, 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 (fees[p.idx] == 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 - 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