結果
問題 | No.1812 Uribo Road |
ユーザー | brthyyjp |
提出日時 | 2022-01-16 13:34:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 716 ms / 5,000 ms |
コード長 | 2,082 bytes |
コンパイル時間 | 298 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 91,844 KB |
最終ジャッジ日時 | 2024-11-23 09:46:15 |
合計ジャッジ時間 | 9,439 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import heapq INF = 10**18 def dijkstra(s, edge): n = len(edge) dist = [INF]*n dist[s] = 0 edgelist = [] heapq.heappush(edgelist,(dist[s], s)) while edgelist: minedge = heapq.heappop(edgelist) if dist[minedge[1]] < minedge[0]: continue v = minedge[1] for e in edge[v]: if dist[e[1]] > dist[v]+e[0]: dist[e[1]] = dist[v]+e[0] heapq.heappush(edgelist,(dist[e[1]], e[1])) return dist def main(): n, m, k = map(int, input().split()) R = list(map(int, input().split())) R = [r-1 for r in R] toid = {} for i, r in enumerate(R): toid[r] = i g = [[] for i in range(n)] ABC = [] for i in range(m): a, b, c = map(int, input().split()) a, b = a-1, b-1 g[a].append((c, b)) g[b].append((c, a)) if i in toid: ABC.append((a, b, c)) dists = dijkstra(0, g) distt = dijkstra(n-1, g) D = [] for i in range(k): a, b = ABC[i][0], ABC[i][1] dista = dijkstra(a, g) distb = dijkstra(b, g) D.append([dista,distb]) dp = [[[INF]*2 for i in range(k)] for i in range(1<<k)] for i, (a, b, c) in enumerate(ABC): dp[1<<i][i][0] = dists[b]+c dp[1<<i][i][1] = dists[a]+c for s in range(1<<k): for i in range(k): if (s>>i)&1: continue ns = s|(1<<i) c = ABC[i][2] for j in range(k): if not ((s>>j)&1): continue if i == j: continue for x in range(2): for y in range(2): dp[ns][i][x] = min(dp[ns][i][x], dp[s][j][y]+D[i][1-x][ABC[j][y]]+c) ans = INF for i in range(k): for x in range(2): v = ABC[i][x] ans = min(ans, dp[(1<<k)-1][i][x]+distt[v]) print(ans) if __name__ == '__main__': main()