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 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 }