Find the shortest travel time from room 1 to room N where entering a hot room requires X minutes since last leaving a cold room, and vice versa.
Medium6Shortest pathGraphDynamic programmingNo attempts yetTime limit2sMemory limit512 MBJOI the snake has wandered into a large mansion. He must escape before any resident of the mansion finds him.
The mansion has N rooms, numbered 1,2,…,N. It also has M corridors, and corridor i (1≤i≤M) connects room Ai and room Bi. JOI can walk a corridor in either direction, and walking corridor i takes Di minutes. There is no way to move between rooms other than through corridors.
The temperature of each room is held constant, and each room is too cold, comfortable, or too hot for JOI. JOI cannot cope with sudden temperature changes, so he cannot enter a too-hot room less than X minutes after he last left a too-cold room. In the same way, he cannot enter a too-cold room less than X minutes after he last left a too-hot room.
While moving, JOI must leave a room immediately after entering it. He also cannot turn back in the middle of a corridor, and he cannot take longer than Di minutes to walk corridor i. He is allowed to enter a room he has already visited and to use a corridor he has already used.
JOI is now in room 1, which is too cold for him. When JOI enters room N, which has the exit, he escapes from the mansion.
Find the minimum time JOI needs to escape from the mansion.
The input consists of 1+N+M lines.
The first line contains three integers N, M, X (2≤N≤10000, 1≤M≤20000, 1≤X≤200) separated by spaces. The mansion has N rooms and M corridors, and JOI needs X minutes to adjust to a temperature change.
The i-th of the next N lines (1≤i≤N) contains an integer Ti (0≤Ti≤2), the temperature of room i. Room i is too cold for JOI if Ti=0, comfortable if Ti=1, and too hot if Ti=2. It is guaranteed that T1=0.
The j-th of the next M lines (1≤j≤M) contains three integers Aj, Bj, Dj (1≤Aj<Bj≤N, 1≤Dj≤200) separated by spaces. Corridor j connects room Aj and room Bj, and walking it takes Dj minutes. Several corridors may connect the same pair of rooms.
The input guarantees that JOI can escape from the mansion.
Print one line with one integer: the minimum number of minutes JOI needs to escape from the mansion.
In example 1, the fastest route visits the rooms in the order 1→2→3→4→5→6→5→8.
In example 2, some pairs of rooms (for example, room 1 and room 5) are connected by more than one corridor.