結果

問題 No.2387 Yokan Factory
ユーザー timi
提出日時 2024-02-14 20:02:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,917 ms / 5,000 ms
コード長 1,280 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 196,524 KB
最終ジャッジ日時 2024-09-28 19:00:18
合計ジャッジ時間 15,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
  from sys import stdin, setrecursionlimit
  input = stdin.readline
  readline = stdin.readline
 
  import heapq
  def dijkstra(p):
      hq=[(0,0)]
      heapq.heapify(hq) # リストを優先度付きキューに変換
      cost=[10**18+1]*N # 行ったことのないところはinf
      cost[0]=0 # 開始地点は0
      while hq:
        c,v=heapq.heappop(hq)
        if N-1==v:
          break
        if c>cost[v]: # コストが現在のコストよりも高ければスルー v:now u:nex
          continue
        for d,u,pp in E[v]:
          if p>pp:
            break
          tmp=d+cost[v]
          if tmp<cost[u]:
            cost[u]=tmp
            heapq.heappush(hq,(tmp,u))
      return cost
  
  N,M,X=map(int,input().split())
  E=[[] for _ in range(N)]
  ma=0
  for i in range(M):
    a,b,t,x=map(int,input().split())
    a-=1;b-=1
    ma=max(ma,x)
    E[a].append((t,b,x))
    E[b].append((t,a,x))
    
  for i in range(N):
    E[i].sort(key=lambda x:x[2],reverse=True)
  
  A=dijkstra(10**9)
  if A[-1]<=X:
    print(10**9)
    exit()
  A=dijkstra(0)
  if A[-1]>X:
    print(-1)
    exit()
  
  
  ok,ng=0,ma+1
  while (ng-ok)>1:
    mid=(ok+ng)//2
    A=dijkstra(mid)
    if A[-1]>X:
      ng=mid
    else:
      ok=mid
  print(ok)
main()
0