結果
問題 | No.1690 Power Grid |
ユーザー | neterukun |
提出日時 | 2021-09-24 22:33:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,212 ms / 3,000 ms |
コード長 | 1,359 bytes |
コンパイル時間 | 204 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 100,812 KB |
最終ジャッジ日時 | 2024-07-05 10:59:17 |
合計ジャッジ時間 | 21,278 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,532 KB |
testcase_01 | AC | 39 ms
52,876 KB |
testcase_02 | AC | 72 ms
75,960 KB |
testcase_03 | AC | 35 ms
52,980 KB |
testcase_04 | AC | 34 ms
52,932 KB |
testcase_05 | AC | 35 ms
52,552 KB |
testcase_06 | AC | 1,129 ms
100,688 KB |
testcase_07 | AC | 1,124 ms
100,696 KB |
testcase_08 | AC | 1,133 ms
100,668 KB |
testcase_09 | AC | 1,139 ms
100,568 KB |
testcase_10 | AC | 1,150 ms
100,812 KB |
testcase_11 | AC | 1,139 ms
100,548 KB |
testcase_12 | AC | 42 ms
52,268 KB |
testcase_13 | AC | 81 ms
76,016 KB |
testcase_14 | AC | 193 ms
78,932 KB |
testcase_15 | AC | 1,212 ms
100,368 KB |
testcase_16 | AC | 1,182 ms
100,628 KB |
testcase_17 | AC | 612 ms
87,272 KB |
testcase_18 | AC | 310 ms
82,472 KB |
testcase_19 | AC | 1,185 ms
100,804 KB |
testcase_20 | AC | 1,208 ms
100,692 KB |
testcase_21 | AC | 1,150 ms
100,276 KB |
testcase_22 | AC | 1,180 ms
100,408 KB |
testcase_23 | AC | 1,165 ms
100,600 KB |
testcase_24 | AC | 1,170 ms
100,540 KB |
testcase_25 | AC | 1,207 ms
100,396 KB |
testcase_26 | AC | 1,204 ms
100,392 KB |
testcase_27 | AC | 38 ms
52,324 KB |
ソースコード
INF = 10 ** 12 def warshall_floyd(matrix): n = len(matrix) dist = [[d for d in row] for row in matrix] for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist def cost(state, v): res = INF for nxt_v in range(n): if state & (1 << nxt_v): res = min(res, dist[v][nxt_v]) if res == INF: return 0 else: return res n, m, k = map(int, input().split()) a = list(map(int, input().split())) edges = [list(map(int, input().split())) for i in range(m)] matrix = [[INF] * n for i in range(n)] for i in range(n): matrix[i][i] = 0 for u, v, c in edges: u -= 1 v -= 1 matrix[u][v] = c matrix[v][u] = c dist = warshall_floyd(matrix) dp = [INF] * (1 << n) dp[0] = 0 states = [bit_state for bit_state in range(1 << n)] states = sorted(states, key=lambda x: bin(x).count("1")) for bit_state in states: for new_v in range(n): if bit_state & (1 << new_v): continue new_state = bit_state | (1 << new_v) dp[new_state] = min(dp[new_state], dp[bit_state] + a[new_v] + cost(bit_state, new_v)) ans = INF for bit_state in states: if bin(bit_state).count("1") == k: ans = min(ans, dp[bit_state]) print(ans)