結果

問題 No.2739 Time is money
ユーザー ID 21712
提出日時 2025-04-21 14:18:35
言語 Go
(1.23.4)
結果
AC  
実行時間 721 ms / 2,000 ms
コード長 1,605 bytes
コンパイル時間 13,304 ms
コンパイル使用メモリ 253,384 KB
実行使用メモリ 39,952 KB
最終ジャッジ日時 2025-04-21 14:19:04
合計ジャッジ時間 28,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import . "fmt"
import . "os"
import bf "bufio"
import "container/heap"
import . "cmp"

func main() {
	rd:=bf.NewReader(Stdin)
	var n,m,x int
	Fscan(rd,&n,&m,&x)
	g := make([][]*Edge, n+1)
	for i:=0; i<m; i++ {
		var u,v,c,t int
		Fscan(rd,&u,&v,&c,&t)
		if u == v {
			continue
		}
		g[u] = append(g[u], &Edge{v,c,t})
		g[v] = append(g[v], &Edge{u,c,t})
	}
	memo := make([]Item, n+1)
	for i := range memo {
		memo[i].id = i
		memo[i].time = 1e18
	}
	memo[0].time = 0
	pq := make(PQ, 0, n+1)
	heap.Push(&pq, &Item{ 1, 0, 0 })
	for pq.Len() > 0 {
		item := heap.Pop(&pq).(*Item)
		if compare(item, &memo[item.id]) > 0  {
			continue
		}
		for _, e := range g[item.id] {
			next := &Item{ e.to, item.money - e.cost, item.time + e.time }
			if next.money < 0 {
				work := (-next.money + x - 1) / x
				next.money += work * x
				next.time += work
			}
			if compare(next, &memo[e.to]) < 0 {
				memo[e.to] = *next
				heap.Push(&pq, next)
			}
		}
	}
	if memo[n].time < 1e18 {
		Println(memo[n].time)
	} else {
		Println(-1)
	}
}

type Edge struct {
	to, cost, time int
}

type Item struct {
	id, money, time int
}

func compare(a, b *Item) int {
	if d := Compare(a.time, b.time); d != 0 {
		return d
	} else {
		return Compare(b.money, a.money)
	}
}

type PQ []*Item
func (pq PQ) Len() int { return len(pq) }
func (pq PQ) Less(i, j int) bool { return compare(pq[i], pq[j]) < 0 }
func (pq PQ) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *PQ) Push(x any) {
	*pq = append(*pq, x.(*Item))
}
func (pq *PQ) Pop() any {
	old := *pq
	n := len(old)-1
	x := old[n]
	*pq = old[:n]
	return x
}
0