結果
問題 | No.614 壊れたキャンパス |
ユーザー |
![]() |
提出日時 | 2020-03-04 10:41:59 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,430 ms / 2,000 ms |
コード長 | 1,604 bytes |
コンパイル時間 | 80 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 89,772 KB |
最終ジャッジ日時 | 2024-10-13 23:35:10 |
合計ジャッジ時間 | 11,918 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#!/usr/bin/env python3# %%import sysread = sys.stdin.buffer.readreadline = sys.stdin.buffer.readlinereadlines = sys.stdin.buffer.readlinesfrom collections import defaultdictINF = 10 ** 18# %%N, M, _, S, T = map(int, readline().split())if M == 0:if N == 1:print(abs(S - T))else:print(-1)exit()m = map(int, read().split())A, B, C = zip(*zip(m, m, m))# %%A_to_BC = [[] for _ in range(N + 1)]for a, b, c in zip(A, B, C):A_to_BC[a].append((b, c))# %%A_to_BC[N].append((T, T))# %%def to_next_building(a, before):event = [(b, c) for b, c in A_to_BC[a]]event += [(key, -value) for key, value in before.items() if value != INF]event += [(-INF, -INF), (INF, -INF)]event.sort()after = defaultdict(lambda: INF)min_const_term = 3 * INFfor n, x in event:if x > 0:newcost = n + min_const_termif after[x] > newcost:after[x] = newcostelse:const_term = -n - xif min_const_term > const_term:min_const_term = const_termmin_const_term = 3 * INFfor n, x in event[::-1]:if x > 0:newcost = -n + min_const_termif after[x] > newcost:after[x] = newcostelse:const_term = n - xif min_const_term > const_term:min_const_term = const_termreturn after# %%dist = defaultdict(int)dist[S] = 0for a in range(1, N + 1):dist = to_next_building(a, dist)answer = dist[T] if dist[T] < INF else -1print(answer)