結果

問題 No.848 なかよし旅行
ユーザー aru aruaru aru
提出日時 2020-11-12 22:07:32
言語 Go
(1.21.3)
結果
AC  
実行時間 766 ms / 2,000 ms
コード長 3,031 bytes
コンパイル時間 11,498 ms
コンパイル使用メモリ 207,452 KB
実行使用メモリ 20,812 KB
最終ジャッジ日時 2023-08-07 20:05:02
合計ジャッジ時間 14,196 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 766 ms
20,812 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 24 ms
7,988 KB
testcase_12 AC 29 ms
7,996 KB
testcase_13 AC 37 ms
8,108 KB
testcase_14 AC 18 ms
5,564 KB
testcase_15 AC 34 ms
8,052 KB
testcase_16 AC 51 ms
12,412 KB
testcase_17 AC 36 ms
10,204 KB
testcase_18 AC 22 ms
7,776 KB
testcase_19 AC 21 ms
5,616 KB
testcase_20 AC 16 ms
4,376 KB
testcase_21 AC 41 ms
10,196 KB
testcase_22 AC 33 ms
8,108 KB
testcase_23 AC 26 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 58 ms
10,364 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 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
}

var N, M, P, Q, T int
var node [][]edge

const inf = int(1e18)

//---------------------------------------------
// priority queue
//---------------------------------------------
type pqi struct{ a, pos 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
}

func dijkstra(s int) []int {
	dist := make([]int, N)
	for i := 0; i < N; i++ {
		dist[i] = inf
	}
	pq := priorityQueue{}
	dist[s] = 0
	heap.Push(&pq, pqi{0, s})
	for len(pq) != 0 {
		cur := pq[0]
		heap.Pop(&pq)
		for _, e := range node[cur.pos] {
			if dist[e.to] > dist[cur.pos]+e.cost {
				dist[e.to] = dist[cur.pos] + e.cost
				heap.Push(&pq, pqi{dist[e.to], e.to})
			}
		}
	}
	return dist
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N, M, P, Q, T = getI(), getI(), getI()-1, getI()-1, getI()
	node = make([][]edge, N)
	for i := 0; i < M; i++ {
		from, to, cost := getI()-1, getI()-1, getI()
		node[from] = append(node[from], edge{to, cost})
		node[to] = append(node[to], edge{from, cost})
	}

	s := dijkstra(0)
	p := dijkstra(P)
	q := dijkstra(Q)

	ans := -1

	d := s[P] + s[Q] + p[Q]
	if d <= T {
		ans = T
	}

	for x := 0; x < N; x++ {
		for y := 0; y < N; y++ {
			d0 := s[x] + p[x] + p[y] + s[y]
			d1 := s[x] + q[x] + q[y] + s[y]
			// out(x, d0, d1, s[x])
			if max(d0, d1) <= T {
				ans = max(ans, s[x]+s[y]+T-max(d0, d1))
			}

		}
	}
	// out(s, p, q)
	out(ans)
}
0