結果
問題 |
No.1320 Two Type Min Cost Cycle
|
ユーザー |
|
提出日時 | 2020-12-18 02:35:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,254 ms / 2,000 ms |
コード長 | 849 bytes |
コンパイル時間 | 269 ms |
コンパイル使用メモリ | 82,172 KB |
実行使用メモリ | 82,144 KB |
最終ジャッジ日時 | 2024-09-21 08:50:04 |
合計ジャッジ時間 | 15,625 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
from heapq import heappush, heappop import sys def input(): return sys.stdin.buffer.readline()[:-1] INF = 10**16 t = int(input()) n, m = map(int, input().split()) adj = [[] for _ in range(n)] for _ in range(m): u, v, w = map(int, input().split()) adj[u-1].append(w * 3000 + v-1) if t == 0: adj[v-1].append(w * 3000 + u-1) ans = INF for x in range(n): dist = [INF for _ in range(n)] prev = [-1 for _ in range(n)] dist[x] = 0 q = [x] while q: z = heappop(q) i, c0 = z % 3000, z // 3000 if dist[i] != c0: continue; for y in adj[i]: j, c1 = y % 3000, y // 3000 if t == 0 and prev[i] == j: continue if dist[j] > c0 + c1: prev[j] = i dist[j] = c0 + c1 heappush(q, j + (c0 + c1) * 3000) else: if t == 0 or j == x: ans = min(ans, c0 + dist[j] + c1) if ans == INF: print(-1) else: print(ans)