結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー sansaquasansaqua
提出日時 2020-05-29 22:02:40
言語 Common Lisp
(sbcl 2.3.8)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 12,113 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 58,756 KB
実行使用メモリ 71,332 KB
最終ジャッジ日時 2024-11-06 04:43:15
合計ジャッジ時間 7,607 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
27,820 KB
testcase_01 AC 10 ms
27,948 KB
testcase_02 AC 110 ms
48,980 KB
testcase_03 AC 188 ms
65,120 KB
testcase_04 AC 184 ms
71,332 KB
testcase_05 AC 105 ms
65,248 KB
testcase_06 AC 105 ms
65,376 KB
testcase_07 AC 42 ms
40,760 KB
testcase_08 AC 134 ms
69,216 KB
testcase_09 AC 21 ms
32,536 KB
testcase_10 AC 64 ms
48,712 KB
testcase_11 AC 48 ms
42,552 KB
testcase_12 AC 42 ms
44,480 KB
testcase_13 AC 140 ms
59,076 KB
testcase_14 AC 150 ms
58,964 KB
testcase_15 AC 178 ms
65,352 KB
testcase_16 AC 87 ms
48,844 KB
testcase_17 AC 207 ms
69,296 KB
testcase_18 AC 64 ms
48,808 KB
testcase_19 AC 182 ms
63,184 KB
testcase_20 AC 49 ms
38,692 KB
testcase_21 AC 76 ms
55,056 KB
testcase_22 AC 179 ms
59,100 KB
testcase_23 AC 14 ms
32,516 KB
testcase_24 AC 15 ms
32,276 KB
testcase_25 AC 39 ms
42,432 KB
testcase_26 AC 92 ms
48,988 KB
testcase_27 AC 94 ms
52,932 KB
testcase_28 AC 183 ms
63,324 KB
testcase_29 AC 28 ms
40,452 KB
testcase_30 AC 176 ms
61,280 KB
testcase_31 AC 112 ms
50,760 KB
testcase_32 AC 76 ms
44,740 KB
testcase_33 AC 196 ms
61,152 KB
testcase_34 AC 63 ms
46,884 KB
testcase_35 AC 180 ms
67,276 KB
testcase_36 AC 13 ms
28,324 KB
testcase_37 AC 13 ms
34,592 KB
testcase_38 AC 12 ms
28,200 KB
testcase_39 AC 13 ms
28,456 KB
testcase_40 AC 12 ms
28,068 KB
testcase_41 AC 216 ms
69,472 KB
testcase_42 AC 63 ms
44,848 KB
testcase_43 AC 110 ms
50,768 KB
testcase_44 AC 42 ms
38,580 KB
testcase_45 AC 110 ms
48,840 KB
testcase_46 AC 12 ms
27,944 KB
testcase_47 AC 11 ms
27,812 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 06 NOV 2024 04:43:06 AM):

; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.261

ソースコード

diff #

(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Binary heap
;;;

(define-condition heap-empty-error (error)
  ((heap :initarg :heap :reader heap-empty-error-heap))
  (:report
   (lambda (condition stream)
     (format stream "Attempted to pop empty heap ~W" (heap-empty-error-heap condition)))))

(defmacro define-binary-heap (name &key (order '#'>) (element-type 'fixnum))
  "Defines a binary heap specialized for the given order and the element
type. This macro defines a structure of the name NAME and relevant functions:
MAKE-<NAME>, <NAME>-PUSH, <NAME>-POP, <NAME>-REINITIALIZE, <NAME>-EMPTY-P,
<NAME>-COUNT, and <NAME>-PEEK."
  (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-empty-p (intern (format nil "~A-EMPTY-P" string-name)))
         (fname-count (intern (format nil "~A-COUNT" string-name)))
         (fname-peek (intern (format nil "~A-PEEK" string-name)))
         (fname-make (intern (format nil "MAKE-~A" string-name)))
         (acc-position (intern (format nil "~A-POSITION" string-name)))
         (acc-data (intern (format nil "~A-DATA" string-name))))
    `(progn
       (locally
           ;; prevent style warnings
           (declare #+sbcl (muffle-conditions style-warning))
         (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 (*)))
           (position 1 :type (integer 1 #.most-positive-fixnum))))

       (declaim #+sbcl (sb-ext:maybe-inline ,fname-push))
       (defun ,fname-push (obj heap)
         "Adds OBJ to HEAP."
         (declare (optimize (speed 3))
                  (type ,name heap))
         (symbol-macrolet ((position (,acc-position heap)))
           (when (>= position (length (,acc-data heap)))
             (setf (,acc-data heap)
                   (adjust-array (,acc-data heap)
                                 (min (- array-total-size-limit 1)
                                      (* position 2)))))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare (optimize (speed 3) (safety 0)))
                        (unless (= pos 1)
                          (let ((parent-pos (ash pos -1)))
                            (when (funcall ,order (aref data pos) (aref data parent-pos))
                              (rotatef (aref data pos) (aref data parent-pos))
                              (update parent-pos))))))
               (setf (aref data position) obj)
               (update position)
               (incf position)
               heap))))

       (declaim #+sbcl (sb-ext:maybe-inline ,fname-pop))
       (defun ,fname-pop (heap)
         "Removes and returns the element at the top of HEAP."
         (declare (optimize (speed 3))
                  (type ,name heap))
         (symbol-macrolet ((position (,acc-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare (optimize (speed 3) (safety 0))
                                 ((integer 1 #.most-positive-fixnum) pos))
                        (let* ((child-pos1 (+ pos pos))
                               (child-pos2 (1+ child-pos1)))
                          (when (<= child-pos1 position)
                            (if (<= child-pos2 position)
                                (if (funcall ,order (aref data child-pos1) (aref data child-pos2))
                                    (unless (funcall ,order (aref data pos) (aref data child-pos1))
                                      (rotatef (aref data pos) (aref data child-pos1))
                                      (update child-pos1))
                                    (unless (funcall ,order (aref data pos) (aref data child-pos2))
                                      (rotatef (aref data pos) (aref data child-pos2))
                                      (update child-pos2)))
                                (unless (funcall ,order (aref data pos) (aref data child-pos1))
                                  (rotatef (aref data pos) (aref data child-pos1))))))))
               (when (= position 1)
                 (error 'heap-empty-error :heap heap))
               (prog1 (aref data 1)
                 (decf position)
                 (setf (aref data 1) (aref data position))
                 (update 1))))))

       (declaim (inline ,fname-reinitialize))
       (defun ,fname-reinitialize (heap)
         "Makes HEAP empty."
         (setf (,acc-position heap) 1)
         heap)

       (declaim (inline ,fname-empty-p))
       (defun ,fname-empty-p (heap)
         "Returns true iff HEAP is empty."
         (= 1 (,acc-position heap)))

       (declaim (inline ,fname-count))
       (defun ,fname-count (heap)
         "Returns the current number of the elements in HEAP."
         (- (,acc-position heap) 1))

       (declaim (inline ,fname-peek))
       (defun ,fname-peek (heap)
         "Returns the topmost element of HEAP."
         (if (= 1 (,acc-position heap))
             (error 'heap-empty-error :heap heap)
             (aref (,acc-data heap) 1))))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(in-package :cl-user)

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(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)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(define-binary-heap heap
  :order (lambda (node1 node2)
           (< (the double-float (car node1))
              (the double-float (car node2))))
  :element-type list)

(defconstant +inf+ most-positive-double-float)

(defun main ()
  (let* ((n (read))
         (m (read))
         (x (- (read) 1))
         (y (- (read) 1))
         (ps (make-array n :element-type 'int32 :initial-element 0))
         (qs (make-array n :element-type 'int32 :initial-element 0))
         (graph (make-array n :element-type 'list :initial-element nil))
         (dists (make-array n :element-type 'double-float :initial-element +inf+))
         (que (make-heap m)))
    (dotimes (i n)
      (setf (aref ps i) (read-fixnum)
            (aref qs i) (read-fixnum)))
    (dotimes (i m)
      (let* ((u (- (read-fixnum) 1))
             (v (- (read-fixnum) 1))
             (dist (sqrt (float (+ (expt (- (aref ps u) (aref ps v)) 2)
                                   (expt (- (aref qs u) (aref qs v)) 2))
                                1d0))))
        (push (cons u dist) (aref graph v))
        (push (cons v dist) (aref graph u))))
    (heap-push (cons 0d0 x) que)
    (setf (aref dists x) 0d0)
    (loop until (heap-empty-p que)
          for (dist . v) = (heap-pop que)
          when (= dist (aref dists v))
          do (loop for (next . cost) in (aref graph v)
                   for next-dist = (+ dist cost)
                   when (< next-dist (aref dists next))
                   do (setf (aref dists next) next-dist)
                      (heap-push (cons next-dist next) que)))
    (println (aref dists y))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5 4
2 4
0 0
-1 -1
1 -1
1 1
-1 1
1 2
1 3
1 4
1 5
"
    "2.828427
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10 23
3 7
93 -63
-100 47
-90 -96
-83 32
-15 6
8 57
92 -68
-34 0
76 51
-29 -70
9 10
6 5
5 4
9 3
4 6
5 9
4 9
4 3
1 7
4 8
8 5
8 10
8 7
6 2
3 8
1 8
10 1
10 6
1 5
2 7
3 10
2 10
9 8
"
    "193.609552
")))
0