結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-04-13 09:52:27 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 263 ms / 3,000 ms |
| コード長 | 5,228 bytes |
| コンパイル時間 | 3,521 ms |
| コンパイル使用メモリ | 85,612 KB |
| 実行使用メモリ | 60,988 KB |
| 最終ジャッジ日時 | 2024-10-09 09:10:25 |
| 合計ジャッジ時間 | 7,982 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
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();
int[] values = new int[n];
for (int i = 0; i < n; i++) {
values[i] = sc.nextInt();
}
long[][] distances = new long[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(distances[i], Long.MAX_VALUE / 2);
distances[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
int c = sc.nextInt();
distances[a][b] = c;
distances[b][a] = c;
}
for (int a = 0; a < n; a++) {
for (int b = 0; b < n; b++) {
for (int c = 0; c < n; c++) {
distances[b][c] = Math.min(distances[b][c], distances[b][a] + distances[a][c]);
}
}
}
ArrayList<Bridge> bridges = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
bridges.add(new Bridge(i, j, distances[i][j]));
}
}
Collections.sort(bridges);
long ans = Long.MAX_VALUE;
UnionFindTree uft = new UnionFindTree(n);
long[] dp = new long[1 << n];
for (int i = 1; i < (1 << n); i++) {
int pop = getPop(i);
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) == 0) {
continue;
}
dp[i] = dp[i ^ (1 << j)] + values[j];
break;
}
if (pop != k) {
continue;
}
long current = dp[i];
uft.clear();
for (Bridge x : bridges) {
if (x.matches(i) && !uft.same(x)) {
uft.unite(x);
current += x.value;
}
}
ans = Math.min(ans, current);
}
System.out.println(ans);
}
static int getPop(int x) {
int pop = 0;
while (x > 0) {
pop += x % 2;
x >>= 1;
}
return pop;
}
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 (x == parents[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(Bridge x) {
return same(x.left, x.right);
}
public void unite(int x, int y) {
if (!same(x, y)) {
parents[find(x)] = find(y);
}
}
public void unite(Bridge x) {
unite(x.left, x.right);
}
}
static class Bridge implements Comparable<Bridge> {
int left;
int right;
long value;
public Bridge(int left, int right, long value) {
this.left = left;
this.right = right;
this.value = value;
}
public int compareTo(Bridge another) {
if (value == another.value) {
return 0;
} else if (value < another.value) {
return -1;
} else {
return 1;
}
}
public boolean matches(int mask) {
return ((mask & (1 << left)) > 0) && ((mask & (1 << right)) > 0);
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
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 int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten