結果

問題 No.1283 Extra Fee
ユーザー dot_haraaidot_haraai
提出日時 2021-06-04 14:17:05
言語 Nim
(2.0.2)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 3,795 bytes
コンパイル時間 5,050 ms
コンパイル使用メモリ 87,340 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2024-11-16 07:21:31
合計ジャッジ時間 10,393 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,172 KB
testcase_01 AC 5 ms
12,668 KB
testcase_02 AC 5 ms
12,740 KB
testcase_03 AC 4 ms
11,360 KB
testcase_04 AC 4 ms
11,468 KB
testcase_05 AC 4 ms
11,200 KB
testcase_06 AC 5 ms
12,284 KB
testcase_07 AC 4 ms
11,560 KB
testcase_08 AC 4 ms
12,328 KB
testcase_09 AC 4 ms
11,040 KB
testcase_10 AC 4 ms
11,892 KB
testcase_11 AC 16 ms
14,832 KB
testcase_12 AC 19 ms
12,324 KB
testcase_13 AC 14 ms
11,780 KB
testcase_14 AC 53 ms
16,824 KB
testcase_15 AC 84 ms
16,840 KB
testcase_16 AC 20 ms
11,712 KB
testcase_17 AC 220 ms
21,248 KB
testcase_18 AC 290 ms
16,820 KB
testcase_19 AC 312 ms
16,808 KB
testcase_20 AC 288 ms
16,764 KB
testcase_21 AC 297 ms
16,740 KB
testcase_22 AC 266 ms
16,768 KB
testcase_23 AC 268 ms
16,652 KB
testcase_24 AC 300 ms
16,696 KB
testcase_25 AC 321 ms
16,804 KB
testcase_26 AC 320 ms
16,776 KB
testcase_27 AC 320 ms
16,708 KB
testcase_28 AC 319 ms
16,712 KB
testcase_29 AC 231 ms
21,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(2, 18) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'times' [UnusedImport]
/home/judge/data/code/Main.nim(2, 37) Warning: imported and not used: 'deques' [UnusedImport]
/home/judge/data/code/Main.nim(1, 25) Warning: imported and not used: 'sequtils' [UnusedImport]
/home/judge/data/code/Main.nim(2, 26) Warning: imported and not used: 'strformat' [UnusedImport]
/home/judge/data/code/Main.nim(1, 41) Warning: imported and not used: 'algorithm' [UnusedImport]
/home/judge/data/code/Main.nim(1, 52) Warning: imported and not used: 'tables' [UnusedImport]
/home/judge/data/code/Main.nim(1, 60) Warning: imported and not used: 'sets' [UnusedImport]
/home/judge/data/code/Main.nim(1, 66) Warning: imported and not used: 'lists' [UnusedImport]
/home/judge/data/code/Main.nim(1, 73) Warning: imported and not used: 'intsets' [UnusedImport]
/home/judge/data/code/Main.nim(2, 8) Warning: imported and not used: 'critbits' [UnusedImport]
/home/judge/data/code/Main.nim(2, 18) Warning: imported and not used: 'future' [UnusedImport]

ソースコード

diff #

import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets
import critbits, future, strformat, deques,heapqueue
template `max=`(x,y) = x = max(x,y)
template `min=`(x,y) = x = min(x,y)
template `mod=`(x,y) = x = x mod y
template scan2 = (scan(), scan())
template scan3 = (scan(), scan())
let read* = iterator: string {.closure.} =
    while true: (for s in stdin.readLine.split: yield s)
proc scan(): int = read().parseInt
proc scanf(): float = read().parseFloat
proc toInt(c:char): int =
    return int(c) - int('0')


const maxn = 500
var
  board:array[maxn,array[maxn,int]]
  costS:array[maxn,array[maxn,int]]
  costG:array[maxn,array[maxn,int]]
  fromS:array[maxn,array[maxn,(int,int)]]
  fromG:array[maxn,array[maxn,(int,int)]]

proc onCompile()=
  for i in 0..<maxn:
    for j in 0..<maxn:
      board[i][j]=0
      costS[i][j]=int.high.div(4)
      costG[i][j]=int.high.div(4)

onCompile()

proc solve():int=
  var
    n = scan()
    m = scan()
    #board = newseqwith(n,newseqwith(n,0))
    #costS = newseqwith(n,newseqwith(n,int.high.div(4)))
    #costG = newseqwith(n,newseqwith(n,int.high.div(4)))
    #fromS = newseqwith(n,newseq[(int,int)](n))
    #fromG = newseqwith(n,newseq[(int,int)](n))
  costS[0][0]=0
  costG[n-1][n-1]=0
  for i in 0..<m:
    var
      (h,w,c)=(scan()-1,scan()-1,scan())
    board[h][w]=c
  var
    q = initHeapQueue[(int,int,int)]()
  q.push((0,0,0))
  #var fix = n^2
  #var touch = newseqwith(n,newseqwith(n,false))
  while q.len>0:
    var
      (c,h,w)=q.pop()
    #if touch[h][w]==false:
      ##touch[h][w] = true
      #fix-=1
      #if fix==0:
        #break
    if c > costS[h][w]:continue
    if h-1>=0:
      if costS[h-1][w] > costS[h][w]+board[h-1][w]+1:
        costS[h-1][w] = costS[h][w]+board[h-1][w]+1
        q.push((costS[h-1][w],h-1,w))
        fromS[h-1][w] = ((h,w))
    if h+1<n:
      if costS[h+1][w] > costS[h][w]+board[h+1][w]+1:
        costS[h+1][w] = costS[h][w]+board[h+1][w]+1
        q.push((costS[h+1][w],h+1,w))
        fromS[h+1][w] = ((h,w))
      
    if w-1>=0:
      if costS[h][w-1] > costS[h][w]+board[h][w-1]+1:
        costS[h][w-1] = costS[h][w]+board[h][w-1]+1
        q.push((costS[h][w-1],h,w-1))
        fromS[h][w-1] = ((h,w))
      
    if w+1<n:
      if costS[h][w+1] > costS[h][w]+board[h][w+1]+1:
        costS[h][w+1] = costS[h][w]+board[h][w+1]+1
        q.push((costS[h][w+1],h,w+1))
        fromS[h][w+1] = ((h,w))
  q.clear()
  q.push((0,n-1,n-1))
  #fix = n^2
  #touch = newseqwith(n,newseqwith(n,false))
  #echo "往路"
  while q.len>0:
    var
      (c,h,w)=q.pop()
    if c > costG[h][w]:continue
    #if touch[h][w]==false:
      #touch[h][w] = true
      #fix-=1
      #if fix==0:
        #break
    if h-1>=0:
      if costG[h-1][w] > costG[h][w]+board[h-1][w]+1:
        costG[h-1][w] = costG[h][w]+board[h-1][w]+1
        q.push((costG[h-1][w],h-1,w))
        fromG[h-1][w] = ((h,w))
    if h+1<n:
      if costG[h+1][w] > costG[h][w]+board[h+1][w]+1:
        costG[h+1][w] = costG[h][w]+board[h+1][w]+1
        q.push((costG[h+1][w],h+1,w))
        fromG[h+1][w] = ((h,w))
      
    if w-1>=0:
      if costG[h][w-1] > costG[h][w]+board[h][w-1]+1:
        costG[h][w-1] = costG[h][w]+board[h][w-1]+1
        q.push((costG[h][w-1],h,w-1))
        fromG[h][w-1] = ((h,w))
      
    if w+1<n:
      if costG[h][w+1] > costG[h][w]+board[h][w+1]+1:
        costG[h][w+1] = costG[h][w]+board[h][w+1]+1
        q.push((costG[h][w+1],h,w+1))
        fromG[h][w+1] = ((h,w))
  #echo "復路"
  result = costS[n-1][n-1]

  for h in 0..<n:
    for w in 0..<n:
      # (h,w)に通行料金がかかる場合
      if board[h][w]>0:
        var
          (fh,fw) = fromS[h][w]
          (th,tw) = fromG[h][w]
        result.min=2+costS[fh][fw]+costG[th][tw]

    

echo solve()
0