結果

問題 No.859 路線A、路線B、路線C
ユーザー aru aruaru aru
提出日時 2020-11-12 21:20:23
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 4,516 bytes
コンパイル時間 12,487 ms
コンパイル使用メモリ 212,180 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 01:50:49
合計ジャッジ時間 14,182 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

type edge struct {
	to, cost int
}

type connect struct {
	from, to, cost int
}

var node [][]edge
var dist []int

//---------------------------------------------
// priority queue
//---------------------------------------------
type pqi struct{ a, e int }

type priorityQueue []pqi

func (pq priorityQueue) Len() int            { return len(pq) }
func (pq priorityQueue) Swap(i, j int)       { pq[i], pq[j] = pq[j], pq[i] }
func (pq priorityQueue) Less(i, j int) bool  { return pq[i].a < pq[j].a }
func (pq *priorityQueue) Push(x interface{}) { *pq = append(*pq, x.(pqi)) }
func (pq *priorityQueue) Pop() interface{} {
	x := (*pq)[len(*pq)-1]
	*pq = (*pq)[0 : len(*pq)-1]
	return x
}

const inf = int(1e18)

func dijkstra(s int) {
	pq := priorityQueue{}
	heap.Push(&pq, pqi{0, s})
	dist[s] = 0
	for len(pq) != 0 {
		cur := pq[0]
		heap.Pop(&pq)
		// out(cur, node[cur.e])
		for _, e := range node[cur.e] {
			if dist[e.to] > dist[cur.e]+e.cost {
				dist[e.to] = dist[cur.e] + e.cost
				heap.Push(&pq, pqi{dist[e.to], e.to})
			}
		}
	}
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	x, y, z := getI(), getI(), getI()
	s0, t0 := getS(), getI()
	s1, t1 := getS(), getI()

	a := []connect{
		{0, 2, 1}, {0, 4, 1}, {2, 4, 1},
		{1, 3, 1}, {1, 5, 1}, {3, 5, 1}}

	// swap
	if t0 > t1 {
		s0, s1 = s1, s0
		t0, t1 = t1, t0
	}

	if s0 == "A" && s1 == "A" {
		a = append(a, connect{0, 6, t0 - 1})
		a = append(a, connect{6, 7, t1 - t0})
		a = append(a, connect{7, 1, x - t1})
		a = append(a, connect{2, 3, y - 1})
		a = append(a, connect{4, 5, z - 1})
	} else if s0 == "B" && s1 == "B" {
		a = append(a, connect{2, 6, t0 - 1})
		a = append(a, connect{6, 7, t1 - t0})
		a = append(a, connect{7, 3, y - t1})
		a = append(a, connect{0, 1, x - 1})
		a = append(a, connect{4, 5, z - 1})
	} else if s0 == "C" && s1 == "C" {
		a = append(a, connect{4, 6, t0 - 1})
		a = append(a, connect{6, 7, t1 - t0})
		a = append(a, connect{7, 5, z - t1})
		a = append(a, connect{0, 1, x - 1})
		a = append(a, connect{2, 3, y - 1})
	} else {
		flgA := false
		flgB := false
		flgC := false
		switch s0 {
		case "A":
			a = append(a, connect{0, 6, t0 - 1})
			a = append(a, connect{6, 1, x - t0})
			flgA = true
		case "B":
			a = append(a, connect{2, 6, t0 - 1})
			a = append(a, connect{6, 3, y - t0})
			flgB = true
		case "C":
			a = append(a, connect{4, 6, t0 - 1})
			a = append(a, connect{6, 5, z - t0})
			flgC = true
		}
		switch s1 {
		case "A":
			a = append(a, connect{0, 7, t1 - 1})
			a = append(a, connect{7, 1, x - t1})
			flgA = true
		case "B":
			a = append(a, connect{2, 7, t1 - 1})
			a = append(a, connect{7, 3, y - t1})
			flgB = true
		case "C":
			a = append(a, connect{4, 7, t1 - 1})
			a = append(a, connect{7, 5, z - t1})
			flgC = true
		}
		if flgA == false {
			a = append(a, connect{0, 1, x - 1})
		}
		if flgB == false {
			a = append(a, connect{2, 3, y - 1})
		}
		if flgC == false {
			a = append(a, connect{4, 5, z - 1})
		}
	}

	node = make([][]edge, 8)
	for _, e := range a {
		node[e.from] = append(node[e.from], edge{e.to, e.cost})
		node[e.to] = append(node[e.to], edge{e.from, e.cost})
	}
	// out(a)
	// out(node)

	dist = make([]int, 8)
	for i := 0; i < 8; i++ {
		dist[i] = inf
	}
	dijkstra(7)
	// out(dist)
	out(dist[6])
}
0