結果

問題 No.1565 Union
ユーザー 小野寺健小野寺健
提出日時 2021-12-19 10:25:36
言語 Go
(1.22.1)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 2,202 bytes
コンパイル時間 11,510 ms
コンパイル使用メモリ 229,388 KB
実行使用メモリ 27,120 KB
最終ジャッジ日時 2024-05-25 04:40:48
合計ジャッジ時間 19,336 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 168 ms
10,096 KB
testcase_11 AC 216 ms
18,460 KB
testcase_12 AC 260 ms
12,376 KB
testcase_13 AC 71 ms
7,724 KB
testcase_14 AC 314 ms
18,744 KB
testcase_15 AC 379 ms
25,020 KB
testcase_16 AC 368 ms
25,016 KB
testcase_17 AC 405 ms
22,920 KB
testcase_18 AC 409 ms
25,020 KB
testcase_19 AC 415 ms
25,000 KB
testcase_20 AC 272 ms
25,008 KB
testcase_21 AC 270 ms
22,844 KB
testcase_22 AC 273 ms
25,008 KB
testcase_23 AC 285 ms
27,120 KB
testcase_24 AC 271 ms
25,008 KB
testcase_25 AC 287 ms
25,012 KB
testcase_26 AC 266 ms
22,848 KB
testcase_27 AC 274 ms
22,848 KB
testcase_28 AC 286 ms
22,848 KB
testcase_29 AC 282 ms
25,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"bufio"
	"os"
	"math"
)

type Cell struct {
	item int
	next *Cell
}

func newCell(item int, cp *Cell) *Cell {
	newcp := new(Cell)
	newcp.item, newcp.next = item, 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(item int) {
	cp := newCell(item, 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() (int, bool) {
	if q.size == 0 {
		return 0, false
	} else {
		item := q.front.item
		q.front = q.front.next
		q.size--
		if q.size == 0 {
			q.rear = nil
		}
		return item, true
	}
}

func (q *Queue) isEmpty() bool {
	return q.size == 0
}

type CellMgr struct {
	top, cp, last *Cell
}

func newCellMgr() *CellMgr {
	cmp := new(CellMgr)
	cmp.top = nil
	cmp.cp = nil
	cmp.last = nil
	return cmp
}
func (this *CellMgr) iterator() *CellMgr{
	this.cp = this.top
	return this
}

func (this *CellMgr) append(item int) {
	ncp := newCell(item, nil)
	if this.top == nil {
		this.top = ncp
		this.last = ncp
	} else {
		this.last.next = ncp
		this.last = ncp
	}
}

func (this *CellMgr) isNext() bool {
	return this.cp != nil
}

func (this *CellMgr) next() (int, bool) {
	cp := this.cp
	if cp != nil {
		this.cp = cp.next
		return cp.item, true
	} else {
		return 0, false
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
    defer w.Flush()
	var N, M int
	fmt.Fscan(r, &N, &M)
	road := make([]*CellMgr, N)
	d := make([]int, N)
	for i := 0; i < N; i++ {
		road[i] = newCellMgr()
		d[i] = math.MaxInt64
	}

	for i := 0; i < M; i++ {
		var a, b int
		fmt.Fscan(r, &a, &b)
		road[a-1].append(b-1)
		road[b-1].append(a-1)
	}

	d[0] = 0
	q := newQueue()
	q.enqueue(0)
	var fin bool = false
	for !fin && !q.isEmpty() {
		v, _ := q.dequeue()
		rp := road[v].iterator()
		for rp.isNext() {
			nv , _ := rp.next()
			if d[nv] > d[v] + 1 {
				d[nv] = d[v] + 1
				q.enqueue(nv)
				if nv == N-1 {
					fin = true
					break
				}
			}
		}
	}
	if fin {
		fmt.Fprintln(w, d[N-1])
	} else {
		fmt.Fprintln(w, -1)
	}
}
0