結果
問題 | No.1812 Uribo Road |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:16:42 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,370 bytes |
コンパイル時間 | 179 ms |
コンパイル使用メモリ | 82,488 KB |
実行使用メモリ | 85,712 KB |
最終ジャッジ日時 | 2025-03-20 21:17:40 |
合計ジャッジ時間 | 9,963 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 TLE * 1 -- * 21 |
ソースコード
import sysfrom heapq import heappush, heappopdef main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr]); ptr +=1M = int(input[ptr]); ptr +=1K = int(input[ptr]); ptr +=1R = list(map(int, input[ptr:ptr+K]))ptr += Krequired_edges = { r: i for i, r in enumerate(R) }adj = [[] for _ in range(N+1)]for edge_idx in range(1, M+1):A = int(input[ptr]); ptr +=1B = int(input[ptr]); ptr +=1C = int(input[ptr]); ptr +=1adj[A].append( (B, C, edge_idx) )adj[B].append( (A, C, edge_idx) )INF = float('inf')full_mask = (1 << K) - 1dist = [ [INF]*(1<<K) for _ in range(N+1) ]dist[1][0] = 0heap = []heappush(heap, (0, 1, 0))while heap:cost, u, mask = heappop(heap)if u == N and mask == full_mask:print(cost)returnif cost > dist[u][mask]:continuefor (v, c, edge_idx) in adj[u]:new_mask = maskif edge_idx in required_edges:bit = required_edges[edge_idx]new_mask |= (1 << bit)new_cost = cost + cif new_cost < dist[v][new_mask]:dist[v][new_mask] = new_costheappush(heap, (new_cost, v, new_mask))print(-1)if __name__ == "__main__":main()