結果

問題 No.160 最短経路のうち辞書順最小
ユーザー chaemonchaemon
提出日時 2019-08-18 19:37:18
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,713 bytes
コンパイル時間 992 ms
コンパイル使用メモリ 61,704 KB
最終ジャッジ日時 2023-09-15 12:35:32
合計ジャッジ時間 1,562 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(67, 9) Error: undeclared identifier: 'newHeapQueue'

ソースコード

diff #

#{{{ header
import algorithm, sequtils, tables, macros, math, sets, strutils
when defined(MYDEBUG):
  import header

proc scanf(formatstr: cstring){.header: "<stdio.h>", varargs.}
proc getchar(): char {.header: "<stdio.h>", varargs.}
proc nextInt(): int = scanf("%lld",addr result)
proc nextFloat(): float = scanf("%lf",addr result)
proc nextString(): string =
  var get = false
  result = ""
  while true:
    var c = getchar()
    if int(c) > int(' '):
      get = true
      result.add(c)
    else:
      if get: break
      get = false
template `max=`*(x,y:typed):void = x = max(x,y)
template `min=`*(x,y:typed):void = x = min(x,y)
template infty(T): untyped = ((T(1) shl T(sizeof(T)*8-2)) - 1)
#}}}

#{{{ Graph
type Edge[T] = object
  src,dst:int
  weight:T
  rev:int

proc `<`[T](l,r:Edge[T]):bool = l.weight < r.weight

proc newEdge[T](src,dst:int,weight:T,rev:int = -1):Edge[T] =
  var e:Edge[T]
  e.src = src
  e.dst = dst
  e.weight = weight
  e.rev = rev
  return e

type Graph[T] = seq[seq[Edge[T]]]

proc newGraph[T](n:int):Graph[T]=
  var g:Graph[T]
  g = newSeqWith(n,newSeq[Edge[T]]())
  return g

proc addBiEdge[T](g:var Graph[T],src,dst:int,weight:T=1):void =
  g[src].add(newEdge(src,dst,weight,g[dst].len))
  g[dst].add(newEdge(dst,src,weight,g[src].len-1))

proc addEdge[T](g:var Graph[T],src,dst:int,weight:T=1):void =
  g[src].add(newEdge(src,dst,weight,-1))
#}}}

import heapqueue

proc cmp2(l,r:Edge[int]):int = 
  if l.dst < r.dst : -1 else: 1

proc shortestPath[T](g:Graph[T], s:int): (seq[T],seq[int]) = 
  var
    n = g.len
    dist = newSeqWith(n,int.infty)
    prev = newSeqWith(n,-1)
    Q = newHeapQueue[Edge[T]]()
  dist[s] = 0
  Q.push(newEdge[int](-2,s,0))
  while Q.len > 0:
    var e = Q.pop()
    if prev[e.dst] != -1: continue
    prev[e.dst] = e.src;
    for f in g[e.dst]:
      var w = e.weight + f.weight;
      if dist[f.dst] > w:
        dist[f.dst] = w;
        Q.push(newEdge(f.src, f.dst, w));
    discard
  return (dist,prev)
proc path(t: int, prev: seq[int]): seq[int] = 
  var
    u = t
  while u >= 0:
    result.add(u)
    u = prev[u]
  result.reverse()


proc main():void =
  var
    N = nextInt()
    M = nextInt()
    S = nextInt()
    G = nextInt()
    a = newSeq[int](M)
    b = newSeq[int](M)
    c = newSeq[int](M)
    g = newGraph[int](N)
  for i in 0..M-1:
    a[i] = nextInt();b[i] = nextInt();c[i] = nextInt()
    g.addBiEdge(a[i],b[i],c[i])
  for u in 0..N-1:
    g[u].sort(cmp2)
  var (dist,_) = shortest_path(g,G)
  var
    cur = S
    ans = newSeq[int]()
  while cur != G:
    ans.add(cur)
    for e in g[cur]:
      if dist[cur] == dist[e.dst] + e.weight:
        cur = e.dst
        break
  ans.add(cur)
  echo ans.join(" ")
  discard

main()
0