結果
問題 | No.95 Alice and Graph |
ユーザー | maspy |
提出日時 | 2020-05-14 18:15:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 4,117 ms / 5,000 ms |
コード長 | 1,634 bytes |
コンパイル時間 | 413 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 90,112 KB |
最終ジャッジ日時 | 2024-09-15 12:45:13 |
合計ジャッジ時間 | 13,501 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,203 ms
89,728 KB |
testcase_01 | AC | 264 ms
89,472 KB |
testcase_02 | AC | 262 ms
89,984 KB |
testcase_03 | AC | 87 ms
86,144 KB |
testcase_04 | AC | 1,639 ms
90,112 KB |
testcase_05 | AC | 1,257 ms
89,984 KB |
testcase_06 | AC | 1,562 ms
89,856 KB |
testcase_07 | AC | 267 ms
89,728 KB |
testcase_08 | AC | 46 ms
53,376 KB |
testcase_09 | AC | 45 ms
53,632 KB |
testcase_10 | AC | 46 ms
53,632 KB |
testcase_11 | AC | 46 ms
53,632 KB |
testcase_12 | AC | 46 ms
54,400 KB |
testcase_13 | AC | 4,117 ms
89,984 KB |
ソースコード
import sys from collections import deque read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, M, K = map(int, readline().split()) m = map(int, read().split()) if K == 0: print(0) exit() G = [[] for _ in range(N)] for u, v in zip(m, m): u -= 1 v -= 1 G[u].append(v) G[v].append(u) def bfs(v): dist = [1000] * N q = deque([v]) dist[v] = 0 while q: v = q.popleft() for w in G[v]: if dist[w] != 1000: continue dist[w] = dist[v] + 1 q.append(w) return dist dist_mat = [bfs(v) for v in range(N)] def extract_subgraph(A): assert A[0] == 0 mat = [] for i in A: mat.append([dist_mat[i][j] for j in A]) return mat dp = [[K + 1] * (K + 1) for _ in range(1 << (K + 1))] def min_hamilton_path(A): mat = extract_subgraph(A) n = len(A) INF = K + 1 global dp dp[1][0] = 0 for s in range(3, 1 << n, 2): for i in range(n): dp[s][i] = INF if not (s & (1 << i)): continue t = s ^ (1 << i) x = INF for j in range(n): if not (t & (1 << j)): continue y = dp[t][j] + mat[i][j] if x > y: x = y dp[s][i] = x full = (1 << n) - 1 return min(dp[full]) A = [0] for n in range(N - 1, 0, -1): A.append(n) if min_hamilton_path(A) > K: A.pop() if len(A) >= K + 1: break answer = sum((1 << x) - 1 for x in A) print(answer)