結果
| 問題 |
No.1676 Coin Trade (Single)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-07-27 10:25:58 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 609 ms / 2,000 ms |
| コード長 | 2,678 bytes |
| コンパイル時間 | 2,941 ms |
| コンパイル使用メモリ | 83,272 KB |
| 実行使用メモリ | 65,388 KB |
| 最終ジャッジ日時 | 2024-07-16 17:16:39 |
| 合計ジャッジ時間 | 13,397 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<ArrayList<Path>> graph = new ArrayList<>();
int[] values = new int[n];
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
values[i] = sc.nextInt();
int m = sc.nextInt();
for (int j = 0; j < m; j++) {
int idx = sc.nextInt() - 1;
if (values[idx] < values[i]) {
graph.get(idx).add(new Path(i, values[i] - values[idx]));
}
}
}
PriorityQueue<Path> queue = new PriorityQueue<>();
long[] gains = new long[n];
Arrays.fill(gains, -1);
queue.add(new Path(0, 0));
while (queue.size() > 0) {
Path p = queue.poll();
if (gains[p.idx] >= p.value) {
continue;
}
gains[p.idx] = p.value;
for (Path x : graph.get(p.idx)) {
queue.add(new Path(x.idx, x.value + p.value));
}
if (p.idx < n - 1) {
queue.add(new Path(p.idx + 1, p.value));
}
}
System.out.println(gains[n - 1]);
}
static class Path implements Comparable<Path> {
int idx;
long value;
public Path(int idx, long value) {
this.idx = idx;
this.value = value;
}
public int compareTo(Path another) {
if (idx == another.idx) {
if (value == another.value) {
return 0;
} else if (value < another.value) {
return 1;
} else {
return -1;
}
} else {
return idx - another.idx;
}
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
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 String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten