package main import ( "fmt" "bufio" "os" "math" ) type Cell struct { items [2]int next *Cell } func newCell(items [2]int, cp *Cell) *Cell { newcp := new(Cell) newcp.items, newcp.next = items, cp return newcp } type Queue struct { size int front, rear *Cell } func newQueue() *Queue { newqp := new(Queue) newqp.size, newqp.front, newqp.rear = 0, nil, nil return newqp } func (q *Queue) enqueue(items [2]int) { cp := newCell(items, nil) if q.size == 0 { q.front = cp q.rear = cp } else { q.rear.next = cp q.rear = cp } q.size++ } func (q *Queue) dequeue() ([2]int, bool) { if q.size == 0 { return [2]int{0, 0}, false } else { items := q.front.items q.front = q.front.next q.size-- if q.size == 0 { q.rear = nil } return items, true } } func (q *Queue) isEmpty() bool { return q.size == 0 } func main() { r := bufio.NewReader(os.Stdin) w := bufio.NewWriter(os.Stdout) defer w.Flush() var N, M int fmt.Fscan(r, &N, &M) cost := make([][][3]int, N) for i := 0; i < N; i++ { cost[i] = make([][3]int, 0) } for i := 0; i < M; i++ { var a, b, c, x int fmt.Fscan(r, &a, &b, &c, &x) cost[a-1] = append(cost[a-1], [3]int{b-1, c, x}) cost[b-1] = append(cost[b-1], [3]int{a-1, c, x}) } d := make([][2]int, N) for i := 0; i < N; i++ { d[i] = [2]int{math.MaxInt64, math.MaxInt64} } d[N-1][0] = 0 q := newQueue() q.enqueue([2]int{N-1, 0}) for !q.isEmpty() { items, _ := q.dequeue() v, x := items[0], items[1] for _, costs := range cost[v] { nv, c, nx := costs[0], costs[1], costs[2] if nx == 0 { if d[nv][x] > d[v][x] + c { d[nv][x] = d[v][x] + c q.enqueue([2]int{nv, x}) } } else { if d[nv][nx] > d[v][x] + c { d[nv][nx] = d[v][x] + c q.enqueue([2]int{nv, nx}) } } } } for i := 0; i < N-1; i++ { fmt.Fprintln(w, d[i][1]) } }