結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-11-09 19:58:23 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,976 ms / 2,000 ms |
| コード長 | 2,638 bytes |
| コンパイル時間 | 2,706 ms |
| コンパイル使用メモリ | 78,676 KB |
| 実行使用メモリ | 98,288 KB |
| 最終ジャッジ日時 | 2024-11-16 07:12:05 |
| 合計ジャッジ時間 | 31,674 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
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));
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 (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;
}
}
}
}
tenten