Submission #4705399


Source Code Expand

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defun read-fixnum ()
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char *standard-input* nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte *standard-input* nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (return-from read-fixnum 0))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (the fixnum (* result 10))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  `(progn
     ,@(mapcar (lambda (form)
                 `(format *error-output* "~A => ~A~%" ',form ,form))
               forms))
  #-swank nil)

(defmacro define-binary-heap (name &key (test '#'>) (element-type 'fixnum))
  (check-type name symbol)
  (let* ((string-name (string name))
         (fname-push (intern (format nil "~A-PUSH" string-name)))
         (fname-pop (intern (format nil "~A-POP" string-name)))
         (fname-reinitialize (intern (format nil "~A-REINITIALIZE" string-name)))
         (fname-peak (intern (format nil "~A-PEAK" string-name)))
         (fname-make (intern (format nil "MAKE-~A" string-name)))
         (acc-next-position (intern (format nil "~A-NEXT-POSITION" string-name)))
         (acc-data (intern (format nil "~A-DATA" string-name))))
    `(progn
       (defstruct (,name
                   (:constructor ,fname-make
                       (size
                        &aux (data ,(if (eql element-type '*)
                                        `(make-array (1+ size))
                                        `(make-array (1+ size) :element-type ',element-type))))))
         (data #() :type (simple-array ,element-type (*)) :read-only t)
         (next-position 1 :type (integer 1 #.most-positive-fixnum)))

       (declaim (inline ,fname-push))
       (defun ,fname-push (obj heap)
         (symbol-macrolet ((next-position (,acc-next-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (unless (= pos 1)
                          (let ((parent-pos (ash pos -1)))
                            (when (funcall ,test (aref data pos) (aref data parent-pos))
                              (rotatef (aref data pos) (aref data parent-pos))
                              (update parent-pos))))))
               (setf (aref data next-position) obj)
               (update next-position)
               (incf next-position)
               heap))))

       (declaim (inline ,fname-pop))
       (defun ,fname-pop (heap &optional (error t) null-value)
         (symbol-macrolet ((next-position (,acc-next-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare ((integer 1 #.most-positive-fixnum) pos))
                        (let* ((child-pos1 (+ pos pos))
                               (child-pos2 (1+ child-pos1)))
                          (when (<= child-pos1 next-position)
                            (if (<= child-pos2 next-position)
                                (if (funcall ,test (aref data child-pos1) (aref data child-pos2))
                                    (unless (funcall ,test (aref data pos) (aref data child-pos1))
                                      (rotatef (aref data pos) (aref data child-pos1))
                                      (update child-pos1))
                                    (unless (funcall ,test (aref data pos) (aref data child-pos2))
                                      (rotatef (aref data pos) (aref data child-pos2))
                                      (update child-pos2)))
                                (unless (funcall ,test (aref data pos) (aref data child-pos1))
                                  (rotatef (aref data pos) (aref data child-pos1))))))))
               (if (= next-position 1)
                   (if error
                       (error "No element in heap.")
                       null-value)
                   (prog1 (aref data 1)
                     (decf next-position)
                     (setf (aref data 1) (aref data next-position))
                     (update 1)))))))

       (declaim (inline ,fname-reinitialize))
       (defun ,fname-reinitialize (heap)
         (setf (,acc-next-position heap) 1)
         heap)

       (declaim (inline ,fname-peak))
       (defun ,fname-peak (heap &optional (error t) null-value)
         (if (= 1 (,acc-next-position heap))
             (if error
                 (error "No element in heap")
                 null-value)
             (aref (,acc-data heap) 1))))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Hauptteil

;; (city . cost)
(define-binary-heap heap
  :test (lambda (x y)
          (< (cdr x) (cdr y)))
  :element-type (cons fixnum fixnum))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (m (read))
         ;; (dest . cost)
         (graph (make-array n :element-type 'list :initial-element nil))
         (pqueue (make-heap (* 28 2 m)))
         (visited (make-array `(,n 4 7) :element-type 'boolean :initial-element nil)))
    (declare (uint15 n m))
    (dotimes (i m)
      (let ((city1 (read-fixnum))
            (city2 (read-fixnum))
            (cost (read-fixnum)))
        (declare (fixnum city1 city2 cost))
        (cond ((= city1 (- n 1))
               (push (cons city1 cost) (aref graph city2)))
              ((= city2 (- n 1))
               (push (cons city2 cost) (aref graph city1)))
              (t
               (push (cons city2 cost) (aref graph city1))
               (push (cons city1 cost) (aref graph city2))))))
    (heap-push (cons 0 0) pqueue)
    (loop for (city . time) of-type (fixnum . fixnum) = (heap-pop pqueue)
          for time4 = (mod time 4)
          for time7 = (mod time 7)
          until (and (= city (- n 1))
                     (or (zerop time4) (zerop time7)))
          unless (aref visited city time4 time7)
          do (setf (aref visited city time4 time7) t)
             (loop for (dest . delta) of-type (fixnum . fixnum) in (aref graph city)
                   do (heap-push (cons dest (the fixnum (+ time delta))) pqueue))
          finally (println time))))

#-swank(main)

Submission Info

Submission Time
Task E - 会場への道
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 100
Code Size 7849 Byte
Status AC
Exec Time 301 ms
Memory 43620 KiB

Judge Result

Set Name sub1 sub2
Score / Max Score 30 / 30 70 / 70
Status
AC × 15
AC × 26
Set Name Test Cases
sub1 sub1/input_0.txt, sub1/input_1.txt, sub1/input_2.txt, sub1/input_20.txt, sub1/input_21.txt, sub1/input_22.txt, sub1/input_23.txt, sub1/input_24.txt, sub1/input_3.txt, sub1/input_4.txt, sub1/input_5.txt, sub1/input_6.txt, sub1/input_7.txt, sub1/input_8.txt, sub1/input_9.txt
sub2 sub2/input_0.txt, sub2/input_1.txt, sub2/input_10.txt, sub2/input_11.txt, sub2/input_12.txt, sub2/input_13.txt, sub2/input_14.txt, sub2/input_15.txt, sub2/input_16.txt, sub2/input_17.txt, sub2/input_18.txt, sub2/input_19.txt, sub2/input_2.txt, sub2/input_20.txt, sub2/input_21.txt, sub2/input_22.txt, sub2/input_23.txt, sub2/input_24.txt, sub2/input_25.txt, sub2/input_3.txt, sub2/input_4.txt, sub2/input_5.txt, sub2/input_6.txt, sub2/input_7.txt, sub2/input_8.txt, sub2/input_9.txt
Case Name Status Exec Time Memory
sub1/input_0.txt AC 147 ms 29284 KiB
sub1/input_1.txt AC 147 ms 29284 KiB
sub1/input_2.txt AC 147 ms 29284 KiB
sub1/input_20.txt AC 148 ms 29288 KiB
sub1/input_21.txt AC 148 ms 29284 KiB
sub1/input_22.txt AC 148 ms 29288 KiB
sub1/input_23.txt AC 149 ms 29288 KiB
sub1/input_24.txt AC 151 ms 29284 KiB
sub1/input_3.txt AC 149 ms 29284 KiB
sub1/input_4.txt AC 149 ms 29280 KiB
sub1/input_5.txt AC 148 ms 29284 KiB
sub1/input_6.txt AC 147 ms 29284 KiB
sub1/input_7.txt AC 149 ms 29288 KiB
sub1/input_8.txt AC 148 ms 29284 KiB
sub1/input_9.txt AC 148 ms 29280 KiB
sub2/input_0.txt AC 148 ms 29284 KiB
sub2/input_1.txt AC 148 ms 29280 KiB
sub2/input_10.txt AC 151 ms 31332 KiB
sub2/input_11.txt AC 199 ms 41576 KiB
sub2/input_12.txt AC 161 ms 39524 KiB
sub2/input_13.txt AC 150 ms 31332 KiB
sub2/input_14.txt AC 301 ms 43620 KiB
sub2/input_15.txt AC 160 ms 39520 KiB
sub2/input_16.txt AC 265 ms 43620 KiB
sub2/input_17.txt AC 156 ms 37476 KiB
sub2/input_18.txt AC 212 ms 43620 KiB
sub2/input_19.txt AC 156 ms 35432 KiB
sub2/input_2.txt AC 149 ms 29284 KiB
sub2/input_20.txt AC 149 ms 29280 KiB
sub2/input_21.txt AC 149 ms 29284 KiB
sub2/input_22.txt AC 149 ms 29280 KiB
sub2/input_23.txt AC 148 ms 29280 KiB
sub2/input_24.txt AC 149 ms 29284 KiB
sub2/input_25.txt AC 151 ms 31336 KiB
sub2/input_3.txt AC 149 ms 29284 KiB
sub2/input_4.txt AC 149 ms 29284 KiB
sub2/input_5.txt AC 148 ms 29280 KiB
sub2/input_6.txt AC 148 ms 29280 KiB
sub2/input_7.txt AC 147 ms 29284 KiB
sub2/input_8.txt AC 147 ms 29284 KiB
sub2/input_9.txt AC 150 ms 29284 KiB