結果
問題 |
No.2712 Play more!
|
ユーザー |
|
提出日時 | 2024-03-31 15:16:43 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,033 bytes |
コンパイル時間 | 236 ms |
コンパイル使用メモリ | 82,424 KB |
実行使用メモリ | 77,872 KB |
最終ジャッジ日時 | 2024-09-30 20:30:20 |
合計ジャッジ時間 | 3,275 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 WA * 6 |
ソースコード
# 標準入力された値を整数に変換する def INT(): return int(input()) # 標準入力された複数の値を整数に変換する def MI(): return map(int, input().split()) # 標準入力された複数の値を整数のリストに変換する def LI(): return list(map(int, input().split())) class UnionFind: def __init__(self, N): self.root = [_ for _ in range(N)] self.rank = [0] * N self.size = [1] * N def find(self, x): if self.root[x] == x: return x else: # 根を予め取得し,経路圧縮する self.root[x] = self.find(self.root[x]) return self.root[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return else: if self.rank[x] > self.rank[y]: self.size[x] += self.size[y] self.root[y] = x else: self.size[y] += self.size[x] self.root[x] = y if self.rank[x] == self.rank[y]: self.rank[y] += 1 def same(self, x, y): return self.find(x) == self.find(y) def getSizeOfSet(self, x): return self.size[self.find(x)] import heapq def dijkstra(n, G, start=0): INF = 10**18 dist = [INF] * n dist[start] = 0 pq = [] heapq.heappush(pq, (0, start)) visited = [False] * n while pq: now = heapq.heappop(pq)[1] visited[now] = True for nxt, cost in G[now]: if dist[nxt] > dist[now] + cost and not visited[nxt]: dist[nxt] = dist[now] + cost heapq.heappush(pq, (dist[now] + cost, nxt)) return dist N, M = MI() A = LI() G = [[] for i in range(N)] dsu = UnionFind(N) for _ in range(M): a, b, c = MI() a -= 1 b -= 1 G[a].append((b, A[b] - c)) if dsu.same(a, b): print("inf") exit(0) dsu.union(a, b) dist = dijkstra(N, G) print(dist[-1] + A[0])