#P17432. [LBA-OI R5 B] 彼方的她

[LBA-OI R5 B] 彼方的她

Background

:::align{center} Her beyond, the flower on the other shore. :::

Aliya

“The distance between us seems to be exactly 1000 light-years. So what I see is the Earth from 1000 years ago. This means that the sunlight shone on you after traveling for 8 minutes, then carried your appearance and traveled alone in the universe for 1000 years, and was finally caught by my eyes. It is very possible that I can see you at this very moment. Therefore, we may have met long ago. When we are both looking up at the starry sky, I might be gazing into your eyes.” — Aliya.

Problem Description

Aliya needs to return to Earth through a wormhole network. The wormhole network is an undirected graph with nn vertices and mm edges. Vertex nn is her starting point, and vertex 11 is Earth. Each edge is of the form (u,v,w)(u, v, w), where ww is the “rollback time” of this wormhole tunnel, meaning it can go back to ww units of time earlier.

She needs to go from nn to 11, and:

  • She cannot traverse the same edge consecutively, i.e., a→b→aa \to b \to a cannot occur.
  • Vertex 11 can only be the endpoint, and it must appear exactly once at the end of the path.
  • Other vertices may be visited multiple times.

Let the total sum of edge weights along the path be WW. Given a target time tt, find min⁡∣W−t∣\min |W - t|.

The edge weight sum is defined as the total “rollback time” of all edges included in the path.

Input Format

The first line contains three integers n,m,tn, m, t.
The next mm lines each contain three integers u,v,wu, v, w, representing an undirected edge.

Output Format

Output one integer, the answer.

4 6 10
1 4 4
2 3 1
1 3 6
2 4 3
3 4 2
1 2 5

0
5 7 13
2 3 3
1 2 1
3 4 1
2 4 2
4 5 3
1 3 2
3 5 2

1
1 0 1
1

Hint

Sample Explanation

  • One valid path for Sample #1 is 4→2→3→14 \rightarrow 2 \rightarrow 3 \rightarrow 1.
  • One valid path for Sample #2 is $5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 2 \rightarrow 1$.
  • The explanation for Sample #3 was lost by Aliya.

Constraints

For all testdata, it is guaranteed that 1≤n≤2×1051 \le n \le 2 \times 10^5, 0≤m≤2×1050 \le m \le 2 \times 10^5, 0≤t≤10000 \le t \le 1000, 0≤mt≤7×1060 \le mt \le 7 \times 10^6, 0≤w≤1060 \le w \le 10^6. Also, vertex 11 and vertex nn are connected, and there are no multiple edges or self-loops.

This problem uses bundled subtasks.

::cute-table{tuack} |Subtask ID|n≤n \le|m≤m \le|t≤t \le|Special Property|Score| |:-:|:-:|:-:|:-:|:-:|:-:| |Subtask #1|No limit|<|<|A|44| |Subtask #2|^|<|<|B|66| |Subtask #3|5050|100100|10001000|None|1010|  |Subtask #4|200200|10001000|300300|^|^| |Subtask #5|500500|80008000|100100|^|^| |Subtask #6|6×1046 \times 10^4|<|8080|^|1515| |Subtask #7|10510^5|<|5050|^|2020| |Subtask #8|No limit|<|<|^|2525|

  • Special Property A: For every undirected edge (u,v,w)(u, v, w), w=0w = 0.
  • Special Property B: t=0t = 0.

This problem’s story is adapted from the visual novel game “Her Beyond”, everyone is welcome to try it.

The background image for this problem has a 6K resolution.

Translated by ChatGPT 5