結果

問題 No.2328 Build Walls
ユーザー tentententen
提出日時 2023-05-30 14:52:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,304 bytes
コンパイル時間 4,146 ms
コンパイル使用メモリ 81,380 KB
実行使用メモリ 66,712 KB
最終ジャッジ日時 2023-08-28 00:37:47
合計ジャッジ時間 17,810 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,660 KB
testcase_01 AC 45 ms
49,912 KB
testcase_02 AC 45 ms
49,624 KB
testcase_03 AC 45 ms
50,032 KB
testcase_04 AC 47 ms
49,896 KB
testcase_05 AC 45 ms
49,628 KB
testcase_06 AC 44 ms
49,636 KB
testcase_07 AC 46 ms
49,560 KB
testcase_08 AC 46 ms
49,488 KB
testcase_09 AC 45 ms
49,656 KB
testcase_10 AC 45 ms
49,620 KB
testcase_11 AC 45 ms
49,576 KB
testcase_12 AC 45 ms
49,532 KB
testcase_13 AC 261 ms
57,496 KB
testcase_14 WA -
testcase_15 AC 469 ms
59,468 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 128 ms
55,536 KB
testcase_20 WA -
testcase_21 AC 225 ms
57,384 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 328 ms
59,900 KB
testcase_29 WA -
testcase_30 AC 345 ms
59,828 KB
testcase_31 AC 335 ms
60,140 KB
testcase_32 WA -
testcase_33 AC 952 ms
62,912 KB
testcase_34 AC 355 ms
59,572 KB
testcase_35 AC 852 ms
62,740 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int MOD;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt() - 2;
        int w = sc.nextInt();
        int[][] field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < h; i++) {
            queue.add(new Path(i * w, 0));
        }
        int[] costs = new int[h * w];
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            int r = p.idx / w;
            int c = p.idx % w;
            if (field[r][c] < 0) {
                continue;
            }
            p.value += field[r][c];
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            if (c >= w - 1) {
                continue;
            }
            if (r > 0) {
                queue.add(new Path(p.idx - w + 1, p.value));
                queue.add(new Path(p.idx - w, p.value));
            }
            queue.add(new Path(p.idx + 1, p.value));
            if (r < h - 1) {
                queue.add(new Path(p.idx + w + 1, p.value));
                queue.add(new Path(p.idx + w, p.value));
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int i = w - 1; i < h * w; i += w) {
            ans = Math.min(ans, costs[i]);
        }
        if (ans == Integer.MAX_VALUE) {
            ans = -1;
        }
        System.out.println(ans);
    }
    
    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 Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0