#P17378. [ECNA 2023] A Walk in the Woods

[ECNA 2023] A Walk in the Woods

Problem Description

Brice Bilson loves to take jogs in a nearby forest known as Orthogonal Woods. The forest gets that name because the paths—all two-way—are laid out along an orthogonal grid, with all turns being 90 degrees. Brice is a bit persnickety when it comes to his jogs, and always follows a set of rules when he reaches an intersection of two or more paths. These rules are

  1. If there are three remaining branches, Brice takes the middle one.

  2. If there are just two remaining branches, Brice takes the one on his left.

  3. If there are no branches to take, Brice ends his jog and walks to the nearest exit.

Brice is persnickety in another way too. He has assigned each path an "interest value", which is a positive integer indicating how interesting that path is to jog. The higher the value, the more interesting the path is. If the value of a path is nn, then Brice will jog on that path no more than nn times in his jog. After the nn-th pass, that path will cease to exist as far as Brice is concerned (so, for example, any three-branch intersection using that path now becomes a two-branch intersection and any two-branch intersection becomes a one-branch intersection). An example is shown in Figure 1 below.

Suppose Brice enters the park at intersection D heading north in the figure on the left, where the numbers next to each path indicate his interest levels. His travels take him on the route DFGCBADFGCBA, at which point we reach the figure on the right, showing the updated interest levels of each path and the "removal" of the path from A to B since it has now been traversed 22 times. From intersection A, Brice now traverses the route ADFGCBEDA, at which point he hits a dead end and ends his jog.

:::align{center} :::

Input Format

Input starts with two integers nn and mm (2≤n≤2 5002 \leq n \leq 2\,500), giving the number of intersections and the number of paths between intersections. The next line contains nn pairs of integers giving the locations of the intersections. Intersections are numbered from 11 to nn in the order they are presented, and all location values x,yx,y satisfy 0≤x,y≤1060 \leq x,y \leq 10^6. After this are mm lines, each containing three integers ii, jj, and kk (1≤i,j≤n1 \leq i,j \leq n, 1≤k≤1061 \leq k \leq 10^6), indicating that a path exists between intersections ii and jj with interest level kk. All paths will be either vertical or horizontal and will not touch any other vertices other than the specified intersection points. The final line of input contains an integer ss (1≤s≤n1 \leq s \leq n) and a character $d \in \{\texttt{N},\texttt{S},\texttt{E},\texttt{W}\}$, indicating that Brice starts his jog by taking the path in direction dd from intersection ss. There will always be a path heading in direction dd from vertex ss.

Output Format

Output the location where Brice ends his jog.

7 8
0 0 5 0 12 0 0 5 5 5 0 10 12 10
1 2 2
2 3 4
4 5 5
6 7 8
1 4 4
2 5 7
3 7 4
4 6 6
4 N
0 0