結果
問題 | No.1690 Power Grid |
ユーザー | tenten |
提出日時 | 2024-08-08 10:44:41 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,142 bytes |
コンパイル時間 | 2,525 ms |
コンパイル使用メモリ | 79,236 KB |
実行使用メモリ | 55,128 KB |
最終ジャッジ日時 | 2024-08-08 10:44:48 |
合計ジャッジ時間 | 6,673 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
50,456 KB |
testcase_01 | AC | 53 ms
50,260 KB |
testcase_02 | AC | 56 ms
50,360 KB |
testcase_03 | AC | 55 ms
50,308 KB |
testcase_04 | AC | 56 ms
50,392 KB |
testcase_05 | AC | 55 ms
50,340 KB |
testcase_06 | AC | 136 ms
54,008 KB |
testcase_07 | AC | 143 ms
54,348 KB |
testcase_08 | AC | 138 ms
53,892 KB |
testcase_09 | AC | 135 ms
53,876 KB |
testcase_10 | AC | 98 ms
53,916 KB |
testcase_11 | AC | 110 ms
54,004 KB |
testcase_12 | AC | 53 ms
50,292 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 96 ms
54,204 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 55 ms
50,116 KB |
ソースコード
import java.io.*;import java.util.*;import java.util.function.*;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 i = 0; i < n; i++) {for (int j = 0; j < n; j++) {for (int a = 0; a < n; a++) {distances[j][a] = Math.min(distances[j][a], distances[j][i] + distances[i][a]);}}}long[] dp = new long[1 << n];long ans = Long.MAX_VALUE;for (int i = 1; i < (1 << n); i++) {int pop = getPop(i);if (pop > k) {continue;}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;}for (int j = 0; j < n; j++) {if ((i & (1 << j)) == 0) {continue;}long current = 0;for (int a = 0; a < n; a++) {if (j == a || (i & (1 << a)) == 0) {continue;}current += distances[j][a];}ans = Math.min(ans, dp[i] + current);}}System.out.println(ans);}static int getPop(int x) {int pop = 0;while (x > 0) {pop += x % 2;x >>= 1;}return pop;}}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();}}}