;; -*- coding:utf-8 -*-

(declaim (optimize (speed 3) (safety 0)))

(defparameter *file-path* *load-pathname*)
(deftype u32 () '(unsigned-byte 32))
(defun clip-to-string ()
  (with-output-to-string (out)
    (sb-ext:run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

(defun test ()
  (with-input-from-string (*standard-input* (clip-to-string))
    (load *file-path*)))


(defstruct (queue (:constructor
		  $make-queue))
  (arr (make-array 1) :type (simple-array t (*)))
  (start 0 :type fixnum)
  (end 0 :type fixnum))

(defun make-queue (size)
  ($make-queue :arr (make-array size :initial-element nil)
	       :start 0
	       :end 0))

(declaim (inline null-queue
		 enqueue
		 dequeue
		 reset-queue))

(defun null-queue (queue)
  "Gibt T zurück, wenn QUEUE leer ist."
  (= (queue-start queue) (queue-end queue)))

(defun enqueue (obj queue)
  (setf (aref (queue-arr queue) (queue-end queue)) obj)
  (incf (queue-end queue))
  queue)

(defun enqueue-list (lst queue)
  (if (null lst)
      queue
      (enqueue-list (cdr lst)
		    (enqueue queue (car lst)))))

(defun dequeue (queue)
  (if (null-queue queue)
      (error "QUEUE ist leer.")
      (prog1 (aref (queue-arr queue) (queue-start queue))
	(incf (queue-start queue)))))

(defun reset-queue (queue)
  (setf (queue-start queue) 0
	(queue-end queue) 0))

(defstruct edge
  (dest 0 :type fixnum)
  (weight 0 :type fixnum))

(defun read-graph (n m)
  (declare (fixnum m))
  (let ((graph (make-array n :element-type 'list :initial-element nil)))
    (dotimes (x m graph)
      (let* ((left (- (the fixnum (read)) 1))
	     (right (- (the fixnum (read)) 1))
	     (dist (read)))
	(push (make-edge :dest right :weight dist)
	      (aref graph left))
	(push (make-edge :dest left :weight (- dist))
	      (aref graph right))))))

(defun find-new-idx (graph table)
  (declare ((simple-array fixnum (*)) table)
	   ((simple-array t (*)) graph))
  (loop for idx from 0 below (length graph)
     do (when (and (aref graph idx)
		   (= (aref table idx) most-negative-fixnum))
	  (return idx))
     finally (return -1)))

(defun valid-p (graph)
  "Breitensuche."
  (declare ((simple-array t (*)) graph))
  (let* ((size (length graph))
	 (queue (make-queue size))
	 (table (make-array size
			    :element-type 'fixnum
			    :initial-element most-negative-fixnum)))
    (loop named main-loop
       for new-idx = (find-new-idx graph table)
       until (= -1 new-idx)
       finally (return-from main-loop t)
       do
	 (reset-queue queue)
	 (enqueue new-idx queue)
	 (setf (aref table new-idx) 0) ; Die erste Zahl ist egal.
	 (loop named sub-loop
	    until (null-queue queue)
	    do (let* ((start-idx (dequeue queue))
		      (start-number (aref table start-idx))
		      (edges (aref graph start-idx)))
		 (dolist (edge edges)
		   (let ((dest-idx (edge-dest edge))
			 (dest-number (+ start-number (edge-weight edge))))
		     (cond ((= most-negative-fixnum (aref table dest-idx))
			    (setf (aref table dest-idx) dest-number)
			    (enqueue dest-idx queue))
			   ((/= (aref table dest-idx) dest-number)
			    (return-from main-loop nil))))))))))
	  
	

(defun main ()
  (let* ((n (read))
	 (m (read))
	 (graph (read-graph n m)))
    (write-line (if (valid-p graph)
		    "Yes"
		    "No"))))
	

(main)


Measured memory_bytes: 88384