結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-10-11 13:10:04 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 396 ms / 3,000 ms |
| コード長 | 2,954 bytes |
| コンパイル時間 | 2,344 ms |
| コンパイル使用メモリ | 77,856 KB |
| 実行使用メモリ | 43,924 KB |
| 最終ジャッジ日時 | 2024-09-15 10:55:13 |
| 合計ジャッジ時間 | 8,982 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
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 kk = sc.nextInt();
int[] values = new int[n];
long[][] costs = new long[n][n];
for (int i = 0; i < n; i++) {
values[i] = sc.nextInt();
Arrays.fill(costs[i], Long.MAX_VALUE / 2);
costs[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
int c = sc.nextInt();
costs[a][b] = c;
costs[b][a] = c;
}
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
costs[j][k] = Math.min(costs[j][k], costs[j][i] + costs[i][k]);
}
}
}
long[] dp = new long[1 << n];
long min = Long.MAX_VALUE;
for (int x = 1; x < (1 << n); x++) {
int pop = getPop(x);
if (pop == 1) {
for (int i = 0; i < n; i++) {
if ((x & (1 << i)) != 0) {
dp[x] = values[i];
}
}
if (pop == kk) {
min = Math.min(min, dp[x]);
}
continue;
} else if (pop > kk) {
continue;
}
dp[x] = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if ((x & (1 << i)) == 0) {
continue;
}
long distance = Long.MAX_VALUE;
for (int j = 0; j < n; j++) {
if ((x & (1 << j)) == 0 || i == j) {
continue;
}
distance = Math.min(distance, costs[i][j]);
}
dp[x] = Math.min(dp[x], dp[x ^ (1 << i)] + values[i] + distance);
}
if (pop == kk) {
min = Math.min(min, dp[x]);
}
}
System.out.println(min);
}
static int getPop(int x) {
int pop = 0;
while (x > 0) {
pop += x % 2;
x >>= 1;
}
return pop;
}
}
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 String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten