#P6573. [BalticOI 2017] Toll

    ID: 7342 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2017线段树矩阵运算O2优化BalticOI(波罗的海)

[BalticOI 2017] Toll

Background

As a qualified freight company, you need to deliver goods while spending as little money as possible.

Problem Description

This city has nn locations, and there are mm edges between these nn locations.
The freight company has received oo orders, and they need to minimize the money spent along the route.

For each road, there are three pieces of information:

  • a,ba, b mean the road connects from aa to bb;
  • tt means how much money this road costs.

For each order, aa and bb are given, meaning you need to transport goods from aa to bb. For each order, find the minimum money needed; if it cannot be delivered, output 1-1.

In particular, for a road with endpoints numbered a,ba, b, it is guaranteed that:

$$\left\lfloor\dfrac{b}{k}\right\rfloor=1+\left\lfloor\dfrac{a}{k}\right\rfloor$$

(kk is a given constant).

Input Format

The first line contains four integers k,n,m,ok, n, m, o, representing a constant, the number of locations, the number of edges, and the number of orders.
The locations are numbered from 00 to n1n - 1.

The next mm lines each contain three integers a,b,ta, b, t, meaning that it costs tt to go from aa to bb. It is guaranteed that $\left\lfloor\dfrac{b}{k}\right\rfloor=1+\left\lfloor\dfrac{a}{k}\right\rfloor$, and between any two locations there is at most 11 edge.

The next oo lines each contain two integers a,ba, b, meaning you need to transport goods from aa to bb.

Output Format

For each order, output one integer per line representing the answer.

5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
15
9
7
8
-1

Hint

Constraints

This problem uses bundled testdata.

  • Subtask 1 (7 pts): k=1k = 1.
  • Subtask 2 (10 pts): For every order, a=0a = 0.
  • Subtask 3 (8 pts): o100o \le 100.
  • Subtask 4 (31 pts): o3000o \le 3000.
  • Subtask 5 (44 pts): No special constraints.

For 100%100\% of the testdata, 1n500001 \le n \le 50000, 1o100001 \le o \le 10000, 1k51 \le k \le 5, 0a<b<n0 \le a < b < n, 1t100001 \le t \le 10000.

Notes

Translated from BOI 2017 D1 T3 Toll.
Translator: @一只书虫仔
This problem enforces O2O2 optimization.

Translated by ChatGPT 5