結果
| 問題 |
No.2630 Colorful Vertices and Cheapest Paths
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-02-21 17:46:41 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 811 ms / 2,500 ms |
| コード長 | 4,013 bytes |
| コンパイル時間 | 2,695 ms |
| コンパイル使用メモリ | 85,764 KB |
| 実行使用メモリ | 60,304 KB |
| 最終ジャッジ日時 | 2024-09-29 04:17:36 |
| 合計ジャッジ時間 | 19,250 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
static List<Set<Integer>> graph;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
Road[] roads = new Road[m];
for (int i = 0; i < m; i++) {
roads[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1);
}
int[] colors = new int[n];
for (int i = 0; i < n; i++) {
colors[i] = sc.nextInt() - 1;
}
int[] values = new int[10];
for (int i = 0; i < 10; i++) {
values[i] = sc.nextInt();
}
int[] mask = new int[m];
for (int i = 0; i < m; i++) {
mask[i] = (1 << colors[roads[i].left]) | (1 << colors[roads[i].right]);
}
int q = sc.nextInt();
Road[] queries = new Road[q];
for (int i = 0; i < q; i++) {
queries[i] = new Road(sc.nextInt() - 1, sc.nextInt() - 1);
}
long[] ans = new long[q];
Arrays.fill(ans, Long.MAX_VALUE);
UnionFindTree uft = new UnionFindTree(n);
long[] dp = new long[1 << 10];
for (int i = 1; i < (1 << 10); i++) {
final int d = i;
int idx = IntStream.range(0, 10).filter(j -> (d & (1 << j)) > 0).findFirst().orElse(0);
dp[d] = dp[d ^ (1 << idx)] + values[idx];
uft.clear();
for (int j = 0; j < m; j++) {
if ((d & (mask[j])) != mask[j]) {
continue;
}
uft.unite(roads[j]);
}
for (int j = 0; j < q; j++) {
if (uft.same(queries[j])) {
ans[j] = Math.min(ans[j], dp[d]);
}
}
}
System.out.println(Arrays.stream(ans).mapToObj(i -> String.valueOf(i == Long.MAX_VALUE ? -1 : i)).collect(Collectors.joining("\n")));
}
static class Road {
int left;
int right;
public Road(int left, int right) {
this.left = left;
this.right = right;
}
}
static class UnionFindTree {
int[] parents;
public UnionFindTree(int size) {
parents = new int[size];
}
public void clear() {
for (int i = 0; i < parents.length; i++) {
parents[i] = i;
}
}
public int find(int x) {
if (parents[x] == x) {
return x;
} else {
return parents[x] = find(parents[x]);
}
}
public boolean same(int x, int y) {
return find(x) == find(y);
}
public boolean same(Road r) {
return same(r.left, r.right);
}
public void unite(int x, int y) {
if (!same(x, y)) {
parents[find(x)] = find(y);
}
}
public void unite(Road r) {
unite(r.left, r.right);
}
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten