結果
| 問題 |
No.1814 Uribo Road (Easy)
|
| ユーザー |
tenten
|
| 提出日時 | 2022-01-21 13:01:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 566 ms / 2,000 ms |
| コード長 | 5,366 bytes |
| コンパイル時間 | 2,240 ms |
| コンパイル使用メモリ | 80,396 KB |
| 実行使用メモリ | 66,576 KB |
| 最終ジャッジ日時 | 2024-11-25 07:34:50 |
| 合計ジャッジ時間 | 13,422 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 44 |
ソースコード
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 m = sc.nextInt();
int k = sc.nextInt();
HashMap<Integer, Integer> idxes = new HashMap<>();
for (int i = 0; i < k; i++) {
idxes.put(sc.nextInt() - 1, i);
}
ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new HashMap<>());
}
HashMap<Integer, Integer> compress = new HashMap<>();
int[] lefts = new int[k];
int[] rights = new int[k];
int[] values = new int[k];
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
int c = sc.nextInt();
if (idxes.containsKey(i)) {
int x = idxes.get(i);
lefts[x] = a;
rights[x] = b;
values[x] = c;
compress.put(a, null);
compress.put(b, null);
} else {
graph.get(a).put(b, c);
graph.get(b).put(a, c);
}
}
compress.put(0, null);
compress.put(n - 1, null);
int count = 0;
int[] cities = new int[compress.size()];
for (int x : compress.keySet()) {
cities[count] = x;
compress.put(x, count++);
}
PriorityQueue<Path> queue = new PriorityQueue<>();
int[][] cost = new int[count][n];
for (int i = 0; i < count; i++) {
Arrays.fill(cost[i], Integer.MAX_VALUE);
queue.add(new Path(cities[i], 0));
while (queue.size() > 0) {
Path p = queue.poll();
if (cost[i][p.idx] <= p.value) {
continue;
}
cost[i][p.idx] = p.value;
for (int x : graph.get(p.idx).keySet()) {
queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
}
}
}
ArrayList<ArrayList<Road>> small = new ArrayList<>();
for (int i = 0; i < count; i++) {
small.add(new ArrayList<>());
}
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
if (i == j) {
continue;
}
small.get(i).add(new Road(j, cost[i][cities[j]], -1));
}
}
for (int i = 0; i < k; i++) {
small.get(compress.get(lefts[i])).add(new Road(compress.get(rights[i]), values[i], i));
small.get(compress.get(rights[i])).add(new Road(compress.get(lefts[i]), values[i], i));
}
long[][] ans = new long[1 << k][count];
for (long[] arr : ans) {
Arrays.fill(arr, Long.MAX_VALUE);
}
PriorityQueue<Road> roads = new PriorityQueue<>();
roads.add(new Road(compress.get(0), 0, 0));
while (roads.size() > 0) {
Road r = roads.poll();
if (ans[r.mask][r.idx] <= r.value) {
continue;
}
for (int i = r.mask; i > 0; i = (r.mask & (i - 1))) {
ans[i][r.idx] = Math.min(ans[i][r.idx], r.value);
}
ans[0][r.idx] = Math.min(ans[0][r.idx], r.value);
for (Road x : small.get(r.idx)) {
if (x.mask >= 0) {
roads.add(new Road(x.idx, r.value + x.value, r.mask | (1 << x.mask)));
} else {
roads.add(new Road(x.idx, r.value + x.value, r.mask));
}
}
}
System.out.println(ans[(1 << k) - 1][compress.get(n - 1)]);
}
static class Path implements Comparable<Path> {
int idx;
int value;
public Path(int idx, int value) {
this.idx = idx;
this.value = value;
}
public int compareTo(Path another) {
return value - another.value;
}
}
static class Road implements Comparable<Road> {
int idx;
long value;
int mask;
public Road(int idx, long value, int mask) {
this.idx = idx;
this.value = value;
this.mask = mask;
}
public int compareTo(Road another) {
if (value == another.value) {
return 0;
} else if (value < another.value) {
return -1;
} else {
return 1;
}
}
}
}
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 {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten