結果

問題 No.1283 Extra Fee
ユーザー ks2mks2m
提出日時 2020-11-06 22:05:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,069 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 72,596 KB
最終ジャッジ日時 2023-08-10 05:58:51
合計ジャッジ時間 18,109 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,844 KB
testcase_01 AC 43 ms
49,540 KB
testcase_02 AC 43 ms
49,660 KB
testcase_03 AC 43 ms
51,544 KB
testcase_04 AC 43 ms
49,524 KB
testcase_05 AC 43 ms
49,512 KB
testcase_06 AC 46 ms
49,756 KB
testcase_07 AC 43 ms
49,756 KB
testcase_08 AC 46 ms
49,660 KB
testcase_09 AC 46 ms
49,724 KB
testcase_10 AC 45 ms
49,548 KB
testcase_11 AC 161 ms
55,908 KB
testcase_12 AC 177 ms
55,844 KB
testcase_13 AC 180 ms
56,248 KB
testcase_14 AC 324 ms
57,688 KB
testcase_15 AC 435 ms
60,172 KB
testcase_16 AC 191 ms
55,336 KB
testcase_17 AC 720 ms
68,356 KB
testcase_18 AC 978 ms
72,380 KB
testcase_19 AC 1,017 ms
72,596 KB
testcase_20 AC 993 ms
72,500 KB
testcase_21 AC 1,019 ms
72,284 KB
testcase_22 AC 1,006 ms
70,596 KB
testcase_23 AC 774 ms
66,256 KB
testcase_24 AC 816 ms
67,780 KB
testcase_25 AC 1,058 ms
71,276 KB
testcase_26 AC 1,035 ms
71,496 KB
testcase_27 AC 1,069 ms
71,496 KB
testcase_28 AC 1,029 ms
71,776 KB
testcase_29 AC 813 ms
69,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;

public class Main {
	static int n, n2;
	static int[] c;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		n2 = n * n;
		c = new int[n2];
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int h = Integer.parseInt(sa[0]) - 1;
			int w = Integer.parseInt(sa[1]) - 1;
			c[h * n + w] = Integer.parseInt(sa[2]);
		}
		br.close();

		long[] d = new long[n2];
		Arrays.fill(d, Long.MAX_VALUE);
		d[0] = 0;
		PriorityQueue<Node> que = new PriorityQueue<Node>();
		Node first = new Node(0, 0);
		que.add(first);
		dijkstra(d, que);

		long[] d2 = new long[n2];
		for (int i = 0; i < n2; i++) {
			d2[i] = d[i] - c[i];
		}
		que = new PriorityQueue<Node>();
		for (int i = 1; i < n2 - 1; i++) {
			que.add(new Node(i, d2[i]));
		}
		dijkstra(d2, que);
		System.out.println(d2[n2 - 1]);
	}

	static void dijkstra(long[] d, PriorityQueue<Node> que) {
		int[] dx = {1, -1, n, -n};
		while (!que.isEmpty()) {
			Node cur = que.poll();
			if (cur.d > d[cur.v]) {
				continue;
			}
			for (int dd : dx) {
				int next = cur.v + dd;
				if (next < 0 || n2 <= next) {
					continue;
				}
				if (next / n != cur.v / n &&
						(dd == -1 || dd == 1)) {
					continue;
				}
				long alt = d[cur.v] + 1 + c[next];
				if (alt < d[next]) {
					d[next] = alt;
					que.add(new Node(next, alt));
				}
			}
		}
	}

	static class Node implements Comparable<Node> {
		int v;
		long d;

		public Node(int v, long d) {
			this.v = v;
			this.d = d;
		}

		public int compareTo(Node o) {
			return Long.compare(d, o.d);
		}
	}
}
0