結果

問題 No.1601 With Animals into Institute
ユーザー 小野寺健
提出日時 2021-12-18 09:05:20
言語 Go
(1.23.4)
結果
AC  
実行時間 638 ms / 3,000 ms
コード長 1,857 bytes
コンパイル時間 14,536 ms
コンパイル使用メモリ 231,712 KB
実行使用メモリ 41,944 KB
最終ジャッジ日時 2024-09-15 09:44:40
合計ジャッジ時間 25,582 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
	}
}
0