結果

問題 No.1601 With Animals into Institute
ユーザー 小野寺健小野寺健
提出日時 2021-12-18 09:05:20
言語 Go
(1.22.1)
結果
AC  
実行時間 528 ms / 3,000 ms
コード長 1,857 bytes
コンパイル時間 14,187 ms
コンパイル使用メモリ 203,076 KB
実行使用メモリ 44,736 KB
最終ジャッジ日時 2023-10-13 12:54:58
合計ジャッジ時間 22,460 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 10 ms
4,348 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 528 ms
42,484 KB
testcase_07 AC 472 ms
38,320 KB
testcase_08 AC 505 ms
44,736 KB
testcase_09 AC 485 ms
38,372 KB
testcase_10 AC 489 ms
38,356 KB
testcase_11 AC 486 ms
38,356 KB
testcase_12 AC 502 ms
38,356 KB
testcase_13 AC 499 ms
42,468 KB
testcase_14 AC 502 ms
38,368 KB
testcase_15 AC 480 ms
38,372 KB
testcase_16 AC 464 ms
38,352 KB
testcase_17 AC 496 ms
42,608 KB
testcase_18 AC 9 ms
4,352 KB
testcase_19 AC 9 ms
4,352 KB
testcase_20 AC 9 ms
4,348 KB
testcase_21 AC 9 ms
4,352 KB
testcase_22 AC 9 ms
4,348 KB
testcase_23 AC 9 ms
4,348 KB
testcase_24 AC 9 ms
4,352 KB
testcase_25 AC 8 ms
4,348 KB
testcase_26 AC 9 ms
4,352 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

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