結果
問題 | No.2915 辺更新価値最大化 |
ユーザー | Shirotsume |
提出日時 | 2024-07-29 19:48:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 527 ms / 2,000 ms |
コード長 | 1,935 bytes |
コンパイル時間 | 412 ms |
コンパイル使用メモリ | 82,500 KB |
実行使用メモリ | 80,024 KB |
最終ジャッジ日時 | 2024-07-29 20:31:03 |
合計ジャッジ時間 | 8,147 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
56,152 KB |
testcase_01 | AC | 44 ms
56,768 KB |
testcase_02 | AC | 46 ms
56,772 KB |
testcase_03 | AC | 48 ms
56,544 KB |
testcase_04 | AC | 56 ms
63,244 KB |
testcase_05 | AC | 49 ms
57,696 KB |
testcase_06 | AC | 56 ms
64,204 KB |
testcase_07 | AC | 186 ms
78,604 KB |
testcase_08 | AC | 214 ms
78,772 KB |
testcase_09 | AC | 215 ms
79,216 KB |
testcase_10 | AC | 204 ms
78,552 KB |
testcase_11 | AC | 206 ms
78,464 KB |
testcase_12 | AC | 209 ms
78,304 KB |
testcase_13 | AC | 350 ms
79,036 KB |
testcase_14 | AC | 362 ms
78,872 KB |
testcase_15 | AC | 424 ms
79,284 KB |
testcase_16 | AC | 498 ms
79,740 KB |
testcase_17 | AC | 375 ms
79,540 KB |
testcase_18 | AC | 368 ms
79,864 KB |
testcase_19 | AC | 403 ms
79,444 KB |
testcase_20 | AC | 401 ms
79,540 KB |
testcase_21 | AC | 309 ms
79,040 KB |
testcase_22 | AC | 150 ms
78,588 KB |
testcase_23 | AC | 209 ms
78,532 KB |
testcase_24 | AC | 114 ms
77,876 KB |
testcase_25 | AC | 527 ms
80,024 KB |
testcase_26 | AC | 146 ms
77,972 KB |
testcase_27 | AC | 405 ms
79,948 KB |
ソースコード
import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 import heapq def Dijkstra(s, graph): n = len(graph) dist = [inf] * n dist[s] = 0 hq = [(0, s)] heapq.heapify(hq) while hq: c, now = heapq.heappop(hq) if c > dist[now]: continue for to, cost in graph[now]: if dist[now] + cost < dist[to]: dist[to] = cost + dist[now] heapq.heappush(hq, (dist[to], + to)) return dist n, m, q = mi() assert 1 <= n <= 10 ** 3 and 1 <= m <= 10 ** 3 and 1 <= q <= 10 ** 3 graph = [[] for _ in range(n)] EDGE = [] for _ in range(m): u, v, c = mi() assert 1 <= u <= n and 1 <= v <= n and -10 ** 3 <= c <= 10 ** 3 assert u != v u -= 1 v -= 1 c *= -1 EDGE.append((u, v, c)) graph[u].append((v, c)) #ベルマンフォード法 dist = [inf] * n dist[0] = 0 for _ in range(n): for j in range(n): for to, c in graph[j]: if dist[to] > dist[j] + c: dist[to] = dist[j] + c for i in range(n): for to, c in graph[j]: assert dist[to] <= dist[j] + c graph2 = [[] for _ in range(n)] EDGE2 = [] d1 = dist[n - 1] for u, v, c in EDGE: assert c + dist[u] - dist[v] >= 0 EDGE2.append((u, v, c + dist[u] - dist[v])) graph2[u].append((v, c + dist[u] - dist[v])) onoff = [1] * m E = li() for e in E: assert 1 <= e <= m e -= 1 if onoff[e]: onoff[e] = 0 graph2[EDGE2[e][0]].remove((EDGE2[e][1], EDGE2[e][2])) else: onoff[e] = 1 graph2[EDGE2[e][0]].append((EDGE2[e][1], EDGE2[e][2])) dist = Dijkstra(0, graph2) if dist[n - 1] == inf: print('NaN') else: print(-(dist[n - 1] + d1))