結果

問題 No.1283 Extra Fee
ユーザー ks2mks2m
提出日時 2020-11-06 22:05:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 892 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 2,325 ms
コンパイル使用メモリ 78,224 KB
実行使用メモリ 72,316 KB
最終ジャッジ日時 2024-04-27 23:17:15
合計ジャッジ時間 15,308 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,260 KB
testcase_01 AC 45 ms
50,272 KB
testcase_02 AC 47 ms
50,256 KB
testcase_03 AC 46 ms
50,412 KB
testcase_04 AC 48 ms
50,296 KB
testcase_05 AC 49 ms
50,248 KB
testcase_06 AC 48 ms
50,296 KB
testcase_07 AC 47 ms
50,160 KB
testcase_08 AC 47 ms
50,312 KB
testcase_09 AC 47 ms
50,272 KB
testcase_10 AC 50 ms
50,232 KB
testcase_11 AC 156 ms
54,912 KB
testcase_12 AC 186 ms
55,312 KB
testcase_13 AC 177 ms
55,356 KB
testcase_14 AC 304 ms
57,580 KB
testcase_15 AC 362 ms
59,956 KB
testcase_16 AC 181 ms
55,284 KB
testcase_17 AC 631 ms
67,968 KB
testcase_18 AC 781 ms
71,880 KB
testcase_19 AC 889 ms
71,912 KB
testcase_20 AC 880 ms
72,040 KB
testcase_21 AC 800 ms
72,272 KB
testcase_22 AC 829 ms
68,016 KB
testcase_23 AC 689 ms
65,292 KB
testcase_24 AC 714 ms
68,400 KB
testcase_25 AC 859 ms
70,792 KB
testcase_26 AC 892 ms
72,316 KB
testcase_27 AC 853 ms
70,860 KB
testcase_28 AC 853 ms
72,264 KB
testcase_29 AC 659 ms
67,444 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