結果

問題 No.1 道のショートカット
ユーザー YamaG Naka
提出日時 2022-04-20 15:09:37
言語 Swift
(6.0.3)
結果
WA  
実行時間 -
コード長 1,619 bytes
コンパイル時間 9,686 ms
コンパイル使用メモリ 138,152 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-06-11 16:07:20
合計ジャッジ時間 11,219 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8 WA * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

let n = Int(readLine()!)!
let c = Int(readLine()!)!
let v = Int(readLine()!)!
let s=readLine()!.split(separator:" ").map({Int($0)!})
let t=readLine()!.split(separator:" ").map({Int($0)!})
let y=readLine()!.split(separator:" ").map({Int($0)!})
let m=readLine()!.split(separator:" ").map({Int($0)!})
var br=[Int:[(Int,Int,Int)]]()
for i in 0..<n {
    let s=s[i]-1,g=t[i]-1,time=m[i],cost=y[i]
    br[s,default:[(Int,Int,Int)]()]+=[(g,time,cost)]
}
func bfs() -> Int {
    let start=0
    var queue = ArraySlice<(Int,Int,Int,Set<Int>)>()
    queue.append((start, 0,0,Set<Int>()))
    var mintime = -1
    while let (curnode, time, cost,explored) = queue.popFirst() {
        if let branches=br[curnode] {
            for (nextnode,timeplus,costplus) in branches {
                if !explored.contains(nextnode) {
                    let newcost=cost+costplus
                    if newcost <= c {
                        let newtime=time+timeplus
                        if nextnode==n-1 {
                            //print("first goal!!")
                            //return newtime
                            if mintime == -1 {
                                mintime=newtime
                            } else {
                                mintime = min(mintime,newtime)
                            }
                        }
                        var newexplored=explored
                        newexplored.insert(nextnode)
                        queue.append((nextnode,newtime,newcost,newexplored))
                    }
                }
            }
        }
    }
    return mintime
}
print(bfs())
0