結果
問題 | No.1690 Power Grid |
ユーザー | tenten |
提出日時 | 2021-10-11 13:10:04 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 374 ms / 3,000 ms |
コード長 | 2,954 bytes |
コンパイル時間 | 3,192 ms |
コンパイル使用メモリ | 74,408 KB |
実行使用メモリ | 55,264 KB |
最終ジャッジ日時 | 2023-10-13 14:07:23 |
合計ジャッジ時間 | 9,752 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
49,168 KB |
testcase_01 | AC | 45 ms
49,260 KB |
testcase_02 | AC | 46 ms
49,316 KB |
testcase_03 | AC | 43 ms
49,520 KB |
testcase_04 | AC | 43 ms
49,160 KB |
testcase_05 | AC | 44 ms
49,284 KB |
testcase_06 | AC | 179 ms
54,536 KB |
testcase_07 | AC | 194 ms
54,596 KB |
testcase_08 | AC | 193 ms
54,596 KB |
testcase_09 | AC | 190 ms
54,256 KB |
testcase_10 | AC | 88 ms
54,288 KB |
testcase_11 | AC | 288 ms
55,128 KB |
testcase_12 | AC | 46 ms
49,532 KB |
testcase_13 | AC | 48 ms
49,236 KB |
testcase_14 | AC | 95 ms
52,464 KB |
testcase_15 | AC | 334 ms
54,152 KB |
testcase_16 | AC | 181 ms
52,260 KB |
testcase_17 | AC | 145 ms
52,788 KB |
testcase_18 | AC | 104 ms
52,400 KB |
testcase_19 | AC | 182 ms
54,528 KB |
testcase_20 | AC | 186 ms
54,652 KB |
testcase_21 | AC | 76 ms
54,116 KB |
testcase_22 | AC | 297 ms
54,824 KB |
testcase_23 | AC | 271 ms
54,564 KB |
testcase_24 | AC | 374 ms
55,096 KB |
testcase_25 | AC | 349 ms
55,024 KB |
testcase_26 | AC | 354 ms
55,264 KB |
testcase_27 | AC | 46 ms
49,144 KB |
ソースコード
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(); } }