luogu#P5201. [USACO19JAN] Shortcut G

[USACO19JAN] Shortcut G

Background

The third Gold problem of the USACO January 2019 contest.

Problem Description

Every evening, Farmer John rings a huge bell to call his cows to the barn for dinner. The cows are eager to get to the barn, so they all travel along the shortest path.

The farm can be described as NN fields (1N10,0001 \leq N \leq 10,000), numbered 1N1 \ldots N for convenience, and the barn is located at field 11. The fields are connected by MM bidirectional paths (N1M50,000N-1 \leq M \leq 50,000). Each path has a travel time, and from every field there exists a route (made of some paths) that can reach the barn.

Field ii has cic_i cows. When they hear the dinner bell, these cows all head to the barn along a path with the minimum total travel time. If multiple paths tie for the minimum total time, the cows choose the lexicographically smallest path (that is, they break ties by, at the first point where the two paths diverge, preferring the one that goes through the smaller-numbered field. So the path through fields 77, 33, 66, 11 is preferred over the path through 77, 55, 11 if they take the same amount of time).

Farmer John worries that the barn is too far from some fields. He computes the travel time for each cow and sums them up, calling this sum the total travel time. He wants to reduce the total travel time as much as possible by adding one extra “shortcut”: a path from the barn (field 11) to some other field of his choice, with travel time TT (1T10,0001 \leq T \leq 10,000). When a cow happens to see this shortcut while traveling to the barn along her usual route, if taking the shortcut would allow her to reach the barn faster, she will take it. Otherwise, she will still follow the original route, even if using the shortcut might reduce her travel time.

Help Farmer John find the maximum possible reduction in total travel time that can be achieved by adding one shortcut.

Input Format

The first line contains NN, MM, and TT. The next NN lines contain c1cNc_1 \ldots c_N, each an integer in the range 010,0000 \ldots 10,000. The next MM lines each contain three integers aa, bb, and tt, describing a path connecting fields aa and bwithtraveltimeb with travel time t$. All travel times are in the range 125,0001 \ldots 25,000.

Output Format

Output the maximum reduction in total travel time that Farmer John can achieve.

5 6 2
1 2 3 4 5
1 2 5
1 3 3
2 4 3
3 4 5
4 5 2
3 5 7
40

Hint

Translated by ChatGPT 5