(use gauche.array) (use data.heap) (define (read-number) (string->number (read-line))) (define (read-numbers) (map string->number (string-split (read-line) #\space))) (let* ((n (read-number)) (c (read-number)) (v (read-number)) (s (read-numbers)) (t (read-numbers)) (y (read-numbers)) (m (read-numbers))) (define graph (make-vector (+ n 1) '())) (define visited (make-vector (+ n 1) #f)) (define times (make-array (shape 0 (+ n 1) 0 (+ c 1)) (ash 1 30))) (define heap (make-binary-heap :key car)) (for-each (lambda (x y z w) (vector-set! graph x (cons (cons y (cons z w)) (vector-ref graph x '())))) s t y m) (array-set! times 1 0 0) (binary-heap-push! heap (cons 0 1)) (let loop () (unless (binary-heap-empty? heap) (let* ((h (binary-heap-pop-min! heap)) (from (cdr h)) (weight (car h))) (vector-set! visited from #t) (let inner ((nodes (vector-ref graph from))) (unless (null? nodes) (let* ((node (car nodes)) (to (car node)) (cost (cadr node)) (time (cddr node)) (update #f)) (for-each (lambda (i) (if (>= (- i cost) 0) (let1 t (+ time (array-ref times from (- i cost))) (when (<= t (array-ref times to i)) (set! update #t) (array-set! times to i t))))) (iota (+ c 1))) (if update (binary-heap-push! heap #?=(cons (+ weight time) to)))) (inner (cdr nodes))))) (loop))) (let1 val (apply min (map (lambda (i) (array-ref times n i)) (iota (+ c 1)))) (print (if (= val (ash 1 30)) -1 val))))