#P3739. [HAOI2014] 走出金字塔

[HAOI2014] 走出金字塔

Description

During an expedition, the archaeologist Dr. Kong was accidentally trapped inside a pyramid. Each room in the pyramid is triangular. Dr. Kong can break through a wall to move to an adjacent room. For example, if he is currently in triangular room (2,2)(2,2), he can break through to triangular room (2,1)(2,1), (2,3)(2,3), or (1,1)(1,1). However, breaking through one wall takes KK minutes, and Dr. Kong’s stamina only lasts for SS minutes.

Fortunately, Dr. Kong has a map of the pyramid and finds that there are many exits. Once he enters a triangular room that has an exit, he can leave the pyramid in 11 minute.

Now, can you help Dr. Kong find an exit that minimizes the total time to escape? If possible, output the remaining stamina time after Dr. Kong escapes (it should be greater than or equal to 00); otherwise, output 1-1.

Input Format

The first line contains four integers N,M,K,SN, M, K, S, where:

  • NN is the number of layers of the pyramid.
  • MM is the number of exits.
  • KK is the time to break through one wall.
  • SS is the number of minutes Dr. Kong’s stamina lasts.

The second line contains two integers Xa,YaX_a, Y_a indicating Dr. Kong’s current position.

From the third line to the (M+2)(M+2)-th line, each line contains two integers Xi,YiX_i, Y_i indicating the coordinates of a triangular room that has an exit.

Output Format

Output the remaining stamina time after Dr. Kong escapes; if it is impossible, output 1-1.

4 2 2 10
2 1
3 5
4 4
3

Hint

Constraints and Conventions

For all testdata, 1N1061 \le N \le 10^6, 0M1040 \le M \le 10^4, 0<K200 < K \le 20, 10S10410 \le S \le 10^4.

All numbers are integers, and values are separated by a single space.

Translated by ChatGPT 5