結果
問題 | No.1690 Power Grid |
ユーザー | tenten |
提出日時 | 2021-10-11 13:10:04 |
言語 | Java21 (openjdk 21) |
結果 |
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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
37,060 KB |
testcase_01 | AC | 54 ms
36,664 KB |
testcase_02 | AC | 55 ms
36,676 KB |
testcase_03 | AC | 54 ms
36,884 KB |
testcase_04 | AC | 53 ms
36,904 KB |
testcase_05 | AC | 53 ms
36,516 KB |
testcase_06 | AC | 219 ms
41,404 KB |
testcase_07 | AC | 207 ms
41,240 KB |
testcase_08 | AC | 211 ms
41,524 KB |
testcase_09 | AC | 214 ms
41,268 KB |
testcase_10 | AC | 102 ms
40,624 KB |
testcase_11 | AC | 373 ms
43,704 KB |
testcase_12 | AC | 52 ms
37,128 KB |
testcase_13 | AC | 55 ms
37,112 KB |
testcase_14 | AC | 132 ms
40,360 KB |
testcase_15 | AC | 294 ms
41,156 KB |
testcase_16 | AC | 206 ms
41,732 KB |
testcase_17 | AC | 243 ms
42,576 KB |
testcase_18 | AC | 152 ms
41,656 KB |
testcase_19 | AC | 251 ms
43,924 KB |
testcase_20 | AC | 222 ms
41,488 KB |
testcase_21 | AC | 90 ms
40,836 KB |
testcase_22 | AC | 394 ms
43,812 KB |
testcase_23 | AC | 316 ms
41,260 KB |
testcase_24 | AC | 396 ms
43,796 KB |
testcase_25 | AC | 371 ms
41,624 KB |
testcase_26 | AC | 338 ms
41,560 KB |
testcase_27 | AC | 53 ms
37,148 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(); } }