Under construction

Still being built. Things may change

Interstellar Train

Time limit1sMemory limit1024 MB

Summary
Find the minimum total cost of train fares plus waiting-meal charges to travel from planet 0 to planet N-1, where each meal must fall in its time window.
Level

Hard8 of 10

Topics
Graph, Shortest path, Greedy
Solved
No attempts yet

Problem

In the year 2992, robots have taken most jobs. Many people now have plenty of free time, and your family has decided to travel between the stars!

There are NN reachable planets, numbered from 00 to N−1N-1, and MM interstellar train routes. Route ii (0≤i<M0 \le i < M) leaves planet X[i]X[i] at time A[i]A[i], arrives at planet Y[i]Y[i] at time B[i]B[i], and costs C[i]C[i]. Trains are the only way to travel between planets. You can only get off a train at its destination planet, and you must board the next train from that same planet. Transfers take no time. Formally, a sequence of trains q[0],q[1],…,q[P]q[0], q[1], \dots, q[P] is valid if and only if for every 1≤k≤P1 \le k \le P, Y[q[k−1]]=X[q[k]]Y[q[k-1]] = X[q[k]] and B[q[k−1]]≤A[q[k]]B[q[k-1]] \le A[q[k]].

Interstellar travel takes a long time, so meals add a significant cost on top of the train fares. Fortunately, interstellar trains provide unlimited food for free. If you take route ii, you can eat any number of meals at no cost at any time between A[i]A[i] and B[i]B[i] (inclusive). While your family waits for the next train on planet ii, however, each meal costs T[i]T[i].

Your family must eat WW meals. The ii-th meal (0≤i<W0 \le i < W) can be eaten instantaneously at any time between L[i]L[i] and R[i]R[i] (inclusive).

At time 00, your family is on planet 00. Find the minimum total cost to reach planet N−1N-1. If it is impossible to reach planet N−1N-1, the answer is −1-1.

Constraints

  • 2≤N≤1052 \le N \le 10^5.
  • 0≤M,W≤1050 \le M, W \le 10^5.
  • 0≤X[i],Y[i]<N0 \le X[i], Y[i] < N, X[i]≠Y[i]X[i] \ne Y[i].
  • 1≤A[i]<B[i]≤1091 \le A[i] < B[i] \le 10^9.
  • 1≤T[i],C[i]≤1091 \le T[i], C[i] \le 10^9.
  • 1≤L[i]≤R[i]≤1091 \le L[i] \le R[i] \le 10^9.

Examples1

  1. Example 1

    Input
    2 0 0
    1 1
    
    Expected output
    -1