結果
| 問題 |
No.1812 Uribo Road
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-01-20 13:59:32 |
| 言語 | Java (openjdk 23) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,150 bytes |
| コンパイル時間 | 2,810 ms |
| コンパイル使用メモリ | 80,288 KB |
| 実行使用メモリ | 748,776 KB |
| 最終ジャッジ日時 | 2024-11-23 17:59:29 |
| 合計ジャッジ時間 | 89,947 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 TLE * 10 MLE * 2 |
ソースコード
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<ArrayList<Road>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
int c = sc.nextInt();
graph.get(a).add(new Road(b, c, idxes.getOrDefault(i, -1)));
graph.get(b).add(new Road(a, c, idxes.getOrDefault(i, -1)));
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(0, 0, 0));
int[][] cost = new int[1 << k][n];
for (int[] arr : cost) {
Arrays.fill(arr, Integer.MAX_VALUE);
}
while (queue.size() > 0) {
Path p = queue.poll();
if (cost[p.mask][p.idx] <= p.value) {
continue;
}
for (int i = p.mask; i > 0; i = (p.mask & (i - 1))) {
cost[i][p.idx] = Math.min(cost[i][p.idx], p.value);
}
cost[0][p.idx] = Math.min(cost[0][p.idx], p.value);
for (Road x : graph.get(p.idx)) {
if (x.mask >= 0) {
queue.add(new Path(x.idx, p.value + x.cost, p.mask | (1 << x.mask)));
} else {
queue.add(new Path(x.idx, p.value + x.cost, p.mask));
}
}
}
System.out.println(cost[(1 << k) - 1][n - 1]);
}
static class Path implements Comparable<Path> {
int idx;
int value;
int mask;
public Path(int idx, int value, int mask) {
this.idx = idx;
this.value = value;
this.mask = mask;
}
public int compareTo(Path another) {
return value - another.value;
}
}
static class Road {
int idx;
int cost;
int mask;
public Road(int idx, int cost, int mask) {
this.idx = idx;
this.cost = cost;
this.mask = mask;
}
}
}
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