結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-16 12:01:00
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 4,824 bytes
コンパイル時間 14,192 ms
コンパイル使用メモリ 213,348 KB
実行使用メモリ 8,748 KB
最終ジャッジ日時 2023-09-25 16:59:36
合計ジャッジ時間 20,413 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 46 ms
6,820 KB
testcase_09 AC 233 ms
8,352 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 9 ms
8,280 KB
testcase_15 AC 120 ms
6,832 KB
testcase_16 AC 48 ms
8,568 KB
testcase_17 AC 38 ms
8,316 KB
testcase_18 AC 34 ms
8,264 KB
testcase_19 WA -
testcase_20 AC 11 ms
6,600 KB
testcase_21 WA -
testcase_22 AC 2 ms
4,376 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 22 ms
6,820 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 24 ms
8,524 KB
testcase_37 AC 5 ms
7,888 KB
testcase_38 AC 31 ms
8,680 KB
testcase_39 AC 6 ms
8,168 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 WA -
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 295 ms
8,336 KB
testcase_44 AC 147 ms
8,332 KB
testcase_45 AC 303 ms
8,344 KB
testcase_46 AC 55 ms
6,744 KB
testcase_47 AC 186 ms
6,868 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 288 ms
7,120 KB
testcase_55 AC 278 ms
7,124 KB
testcase_56 AC 280 ms
7,108 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	const INF int = int(1e18)
	const MOD int = 998244353

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, N, M int
	fmt.Fscan(in, &T, &N, &M)
	graph := make([][]Edge, N)
	edges := make([]Edge, 0, M)

	for i := 0; i < M; i++ {
		var u, v, w int
		fmt.Fscan(in, &u, &v, &w)
		u--
		v--
		edges = append(edges, Edge{u, v, w})
		graph[u] = append(graph[u], Edge{u, v, w})
		if T == 0 {
			graph[v] = append(graph[v], Edge{v, u, w})
		}
	}

	res := Mincostcycle(graph, edges)
	fmt.Fprintln(out, res)
}

const INF int = 1e18

func Mincostcycle(graph [][]Edge, edges []Edge) int {
	n, m := len(graph), len(edges)
	max_ := 0
	res := INF

	for i := 0; i < m; i++ {
		e := edges[i]
		cost := e.weight
		from, to := e.from, e.to
		gi := make([][]Edge, n)
		for j := 0; j < m; j++ {
			if i != j {
				e := edges[j]
				gi[e.from] = append(gi[e.from], e)
			}
		}

		var x int
		if max_ <= 1 {
			x = bfs01(n, gi, from)[to]
		} else {
			x = dijkstra(n, gi, from)[to]
		}
		if x == -1 {
			x = INF
		}
		res = min(res, cost+x)
	}

	if res == INF {
		return -1
	}
	return res
}

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

type Edge struct{ from, to, weight int }

func bfs01(n int, adjList [][]Edge, start int) (dist []int) {
	dist = make([]int, n)
	for i := range dist {
		dist[i] = INF
	}

	queue := Deque{}
	queue.Append(start)
	dist[start] = 0
	for !queue.Empty() {
		cur := queue.PopLeft()
		for _, e := range adjList[cur] {
			next, cost := e.to, e.weight
			if dist[next] > dist[cur]+cost {
				dist[next] = dist[cur] + cost
				if cost == 0 {
					queue.AppendLeft(next)
				} else {
					queue.Append(next)
				}
			}
		}
	}

	return
}

type D = int
type Deque struct{ l, r []D }

func (q Deque) Empty() bool {
	return len(q.l) == 0 && len(q.r) == 0
}

func (q Deque) Size() int {
	return len(q.l) + len(q.r)
}

func (q *Deque) AppendLeft(v D) {
	q.l = append(q.l, v)
}

func (q *Deque) Append(v D) {
	q.r = append(q.r, v)
}

func (q *Deque) PopLeft() (v D) {
	if len(q.l) > 0 {
		q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1]
	} else {
		v, q.r = q.r[0], q.r[1:]
	}
	return
}

func (q *Deque) Pop() (v D) {
	if len(q.r) > 0 {
		q.r, v = q.r[:len(q.r)-1], q.r[len(q.r)-1]
	} else {
		v, q.l = q.l[0], q.l[1:]
	}
	return
}

func (q Deque) Front() D {
	if len(q.l) > 0 {
		return q.l[len(q.l)-1]
	}
	return q.r[0]
}

func (q Deque) Back() D {
	if len(q.r) > 0 {
		return q.r[len(q.r)-1]
	}
	return q.l[0]
}

// 0 <= i < q.Size()
func (q Deque) At(i int) D {
	if i < len(q.l) {
		return q.l[len(q.l)-1-i]
	}
	return q.r[i-len(q.l)]
}

func dijkstra(n int, adjList [][]Edge, start int) (dist []int) {
	dist = make([]int, n)
	for i := range dist {
		dist[i] = INF
	}
	dist[start] = 0

	pq := NewHeap(func(a, b H) int {
		return a.dist - b.dist
	}, []H{{start, 0}})

	for pq.Len() > 0 {
		curNode := pq.Pop()
		cur, curDist := curNode.node, curNode.dist
		if curDist > dist[cur] {
			continue
		}

		for _, edge := range adjList[cur] {
			next, weight := edge.to, edge.weight
			if cand := curDist + weight; cand < dist[next] {
				dist[next] = cand
				pq.Push(H{next, cand})
			}
		}
	}

	return
}

type H = struct{ node, dist int }

// Should return a number:
//    negative , if a < b
//    zero     , if a == b
//    positive , if a > b
type Comparator func(a, b H) int

func NewHeap(comparator Comparator, nums []H) *Heap {
	nums = append(nums[:0:0], nums...)
	heap := &Heap{comparator: comparator, data: nums}
	heap.heapify()
	return heap
}

type Heap struct {
	data       []H
	comparator Comparator
}

func (h *Heap) Push(value H) {
	h.data = append(h.data, value)
	h.pushUp(h.Len() - 1)
}

func (h *Heap) Pop() (value H) {
	if h.Len() == 0 {
		return
	}

	value = h.data[0]
	h.data[0] = h.data[h.Len()-1]
	h.data = h.data[:h.Len()-1]
	h.pushDown(0)
	return
}

func (h *Heap) Peek() (value H) {
	if h.Len() == 0 {
		return
	}
	value = h.data[0]
	return
}

func (h *Heap) Len() int { return len(h.data) }

func (h *Heap) heapify() {
	n := h.Len()
	for i := (n >> 1) - 1; i > -1; i-- {
		h.pushDown(i)
	}
}

func (h *Heap) pushUp(root int) {
	for parent := (root - 1) >> 1; parent >= 0 && h.comparator(h.data[root], h.data[parent]) < 0; parent = (root - 1) >> 1 {
		h.data[root], h.data[parent] = h.data[parent], h.data[root]
		root = parent
	}
}

func (h *Heap) pushDown(root int) {
	n := h.Len()
	for left := (root<<1 + 1); left < n; left = (root<<1 + 1) {
		right := left + 1
		minIndex := root

		if h.comparator(h.data[left], h.data[minIndex]) < 0 {
			minIndex = left
		}

		if right < n && h.comparator(h.data[right], h.data[minIndex]) < 0 {
			minIndex = right
		}

		if minIndex == root {
			return
		}

		h.data[root], h.data[minIndex] = h.data[minIndex], h.data[root]
		root = minIndex
	}
}
0