結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  ks2m | 
| 提出日時 | 2020-11-06 22:05:16 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,006 ms / 2,000 ms | 
| コード長 | 1,842 bytes | 
| コンパイル時間 | 3,647 ms | 
| コンパイル使用メモリ | 77,892 KB | 
| 実行使用メモリ | 72,232 KB | 
| 最終ジャッジ日時 | 2024-11-16 06:40:22 | 
| 合計ジャッジ時間 | 17,335 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
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);
		}
	}
}
            
            
            
        