Submission #4827016
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
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
(declare #.OPT)
(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
;; (return-from read-fixnum 0)
(error "Read EOF or #\Nul."))
((= 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 dotimes-unroll ((var count size &optional result) &body body)
(let ((base (gensym))
(whole (gensym))
(_count (gensym)))
(check-type var symbol)
(check-type size (integer 1 #.most-positive-fixnum))
`(block ,whole
(let ((,_count ,count)
(,base 0))
(declare ((unsigned-byte 62) ,base ,_count))
(loop
(when (> (+ ,base ,size) ,_count)
(do ((,var ,base (1+ ,var)))
((>= ,var ,_count)
(return-from ,whole ,result))
,@body))
,@(loop for i from 0 below size
collect `(let ((,var (+ ,base ,i))) ,@body))
(setq ,base (+ ,base ,size)))))))
(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)
;; 0<=s_i<=p_i (i = 0, ..., n-1)
;; 0<=t_i<=q_i (i = 0, ..., n-1)
;; a_k <= s_(x_k)+t_(y_k) <= b_k (k = 0, ..., m-1)
;; t'_(y_k) := -t_(y_k).
;; 0<=s_i<=p_i (i = 0, ..., n-1)
;; -q_i<=t'_i<=0 (i = 0, ..., n-1)
;; a_k <= s_(x_k)-t'_(y_k) <= b_k (k = 0, ..., m-1)
;; I: start point
;; s_i (t'_i) be the minimal distance from I to the vertex i (n+i).
;; 0-s_i <= 0 (edge from vertex i to I with cost 0)
;; s_i-0 <= p_i (I -> i with cost p_i)
;; 0-t'_i <= q_i (i+n -> I with cost q_i)
;; t'_i-0 <= 0 (I -> i+n with cost 0)
;; s_(x_k)-t'_(y_k) <= b_k (n+y_k -> x_k with cost b_k)
;; t'_(y_k)-s_(x_k) <= -a_k (x_k -> n+y_k with cost -a_k)
;; This program is feasible iff there is no negative cycle passing through I.
(defun main ()
(declare #.OPT)
(let* ((n (read))
(m (read))
(ps (make-array n :element-type 'uint31))
(qs (make-array n :element-type 'uint31))
(graph-size (1+ (* 2 n)))
(edge-num (+ (* 4 n) (* 2 m)))
(edge-from (make-array edge-num :element-type 'uint15 :initial-element 0))
(edge-to (make-array edge-num :element-type 'uint15 :initial-element 0))
(edge-cost (make-array edge-num :element-type 'int31 :initial-element 0))
(index 0) ;; edge index
(start (- graph-size 1))
(table (make-array graph-size :element-type 'int32 :initial-element #.(- (expt 2 31) 1))))
(declare (uint31 n m index))
(labels ((add-edge (from to cost)
(setf (aref edge-from index) from)
(setf (aref edge-to index) to)
(setf (aref edge-cost index) cost)
(incf index)))
(declare (inline add-edge))
(setf (aref table start) 0)
(dotimes (i n) (setf (aref ps i) (read-fixnum)))
(dotimes (i n) (setf (aref qs i) (read-fixnum)))
(dotimes (i n)
(add-edge i start 0)
(add-edge start i (aref ps i))
(add-edge (+ i n) start (aref qs i))
(add-edge start (+ i n) 0))
(dotimes (k m)
(let ((x (- (read-fixnum) 1))
(y (- (read-fixnum) 1))
(a (read-fixnum))
(b (read-fixnum)))
(declare (uint15 x y)
(uint31 a b))
(add-edge (+ n y) x b)
(add-edge x (+ n y) (- a))))
(dotimes (_ (- graph-size 1))
(let (updated-p)
(dotimes-unroll (i edge-num 8)
(declare (uint32 i))
(let* ((from-vertex (aref edge-from i))
(to-vertex (aref edge-to i))
(current-cost (aref table from-vertex)))
(unless (= current-cost #.(- (expt 2 31) 1))
(let ((new-cost (+ current-cost (aref edge-cost i))))
(when (< new-cost (aref table to-vertex))
(setf (aref table to-vertex) new-cost
updated-p t))))))
(unless updated-p
(write-line "yes")
(return-from main))))
(write-line "no"))))
#-swank(main)
Submission Info
Submission Time |
|
Task |
H - Asteroids2 |
User |
sansaqua |
Language |
Common Lisp (SBCL 1.1.14) |
Score |
200 |
Code Size |
5886 Byte |
Status |
AC |
Exec Time |
1188 ms |
Memory |
23016 KiB |
Judge Result
Set Name |
All |
Score / Max Score |
200 / 200 |
Status |
|
Set Name |
Test Cases |
All |
00-sample-00, 00-sample-01, 10-small_yes-00, 10-small_yes-01, 10-small_yes-02, 10-small_yes-03, 10-small_yes-04, 10-small_yes-05, 10-small_yes-06, 10-small_yes-07, 10-small_yes-08, 20-small_disturb-00, 20-small_disturb-01, 20-small_disturb-02, 20-small_disturb-03, 20-small_disturb-04, 20-small_disturb-05, 20-small_disturb-06, 20-small_disturb-07, 20-small_disturb-08, 30-large_yes-00, 30-large_yes-01, 30-large_yes-02, 30-large_yes-03, 30-large_yes-04, 40-large_disturb-00, 40-large_disturb-01, 40-large_disturb-02, 40-large_disturb-03, 40-large_disturb-04, 40-large_disturb-05, 40-large_disturb-06, 40-large_disturb-07, 40-large_disturb-08, 40-large_disturb-09, 40-large_disturb-10, 40-large_disturb-11, 40-large_disturb-12, 40-large_disturb-13, 40-large_disturb-14, 40-large_disturb-15 |
Case Name |
Status |
Exec Time |
Memory |
00-sample-00 |
AC |
109 ms |
20964 KiB |
00-sample-01 |
AC |
108 ms |
20964 KiB |
10-small_yes-00 |
AC |
107 ms |
20964 KiB |
10-small_yes-01 |
AC |
107 ms |
20964 KiB |
10-small_yes-02 |
AC |
108 ms |
20968 KiB |
10-small_yes-03 |
AC |
108 ms |
20964 KiB |
10-small_yes-04 |
AC |
107 ms |
20964 KiB |
10-small_yes-05 |
AC |
107 ms |
20964 KiB |
10-small_yes-06 |
AC |
111 ms |
20960 KiB |
10-small_yes-07 |
AC |
110 ms |
20964 KiB |
10-small_yes-08 |
AC |
111 ms |
20964 KiB |
20-small_disturb-00 |
AC |
108 ms |
20964 KiB |
20-small_disturb-01 |
AC |
107 ms |
20964 KiB |
20-small_disturb-02 |
AC |
107 ms |
20960 KiB |
20-small_disturb-03 |
AC |
108 ms |
20968 KiB |
20-small_disturb-04 |
AC |
107 ms |
20960 KiB |
20-small_disturb-05 |
AC |
108 ms |
20964 KiB |
20-small_disturb-06 |
AC |
125 ms |
20960 KiB |
20-small_disturb-07 |
AC |
125 ms |
20968 KiB |
20-small_disturb-08 |
AC |
124 ms |
20960 KiB |
30-large_yes-00 |
AC |
148 ms |
23016 KiB |
30-large_yes-01 |
AC |
148 ms |
23008 KiB |
30-large_yes-02 |
AC |
148 ms |
23008 KiB |
30-large_yes-03 |
AC |
149 ms |
23012 KiB |
30-large_yes-04 |
AC |
149 ms |
23016 KiB |
40-large_disturb-00 |
AC |
1157 ms |
23008 KiB |
40-large_disturb-01 |
AC |
1157 ms |
23008 KiB |
40-large_disturb-02 |
AC |
1162 ms |
23016 KiB |
40-large_disturb-03 |
AC |
1181 ms |
23008 KiB |
40-large_disturb-04 |
AC |
1153 ms |
23008 KiB |
40-large_disturb-05 |
AC |
1159 ms |
23008 KiB |
40-large_disturb-06 |
AC |
1161 ms |
23008 KiB |
40-large_disturb-07 |
AC |
1178 ms |
23008 KiB |
40-large_disturb-08 |
AC |
1155 ms |
23012 KiB |
40-large_disturb-09 |
AC |
1154 ms |
23016 KiB |
40-large_disturb-10 |
AC |
1164 ms |
23012 KiB |
40-large_disturb-11 |
AC |
1178 ms |
23008 KiB |
40-large_disturb-12 |
AC |
1161 ms |
23016 KiB |
40-large_disturb-13 |
AC |
1158 ms |
23012 KiB |
40-large_disturb-14 |
AC |
1163 ms |
23016 KiB |
40-large_disturb-15 |
AC |
1188 ms |
23012 KiB |