結果

問題 No.848 なかよし旅行
ユーザー aru aruaru aru
提出日時 2020-11-12 21:59:29
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,974 bytes
コンパイル時間 11,364 ms
コンパイル使用メモリ 209,796 KB
実行使用メモリ 20,812 KB
最終ジャッジ日時 2023-09-30 01:51:12
合計ジャッジ時間 15,954 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 762 ms
20,812 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 27 ms
8,088 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
4,376 KB
testcase_25 WA -
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 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++ {
		d0 := (p[x] + s[x]) * 2
		d1 := (q[x] + s[x]) * 2
		// out(x, d0, d1, s[x])
		if max(d0, d1) <= T {
			ans = max(ans, s[x]*2+T-max(d0, d1))
		}
	}
	// out(s, p, q)
	out(ans)
}
0