This page is still under construction.

Parts of this page are still being built. What you see may change.

Marble Launchers

Time limit1sMemory limit1024 MB

Summary
Rotate marble launchers at per-45-degree cost so a marble fired from launcher s reaches launcher e, minimizing total rotation cost and outputting the path.
Level

Hard8 of 10

Topics
Graph, Shortest path, Hash map, Implementation
Solved
No attempts yet

Problem

There are NN marble launchers on the coordinate plane. A marble fired from a launcher travels infinitely until it meets a launcher. If the marble meets a launcher while moving, it is fired again in the direction the launcher faces, and it cannot pass by a launcher without visiting it. A marble launcher can face one of 88 directions: north, northeast, east, southeast, south, southwest, west, and northwest. You can also rotate a launcher clockwise by any amount, and the cost to rotate launcher ii by 4545 degrees is cic_{i}. A launcher can fire a marble at most once. Therefore, if a marble visits a launcher that has already been used, the marble stops at that launcher and moves no further.

By rotating the launchers appropriately, you want the marble to travel from launcher ss to launcher ee at minimum cost. Write a program that finds the minimum cost and which launchers the marble passed through.

Input

The first line gives the integers NN, ss, and ee. (2≤N≤100 000,1≤s,e≤N,s≠e2 \leq N \leq 100\,000, 1 \leq s, e \leq N, s \ne e)

The next NN lines give the information for launcher ii: xix_{i}, yiy_{i}, cic_{i}, and did_{i}, separated by spaces.

  • xix_{i} and yiy_{i} are the xx-coordinate and yy-coordinate of launcher ii. (1≤xi,yi≤1091 \leq x_i, y_i \leq 10^{9})
  • cic_{i} is the cost to rotate launcher ii. (0≤ci≤200 0000 \leq c_i \leq 200\,000)
  • did_{i} is the direction launcher ii initially faces. It is one of the uppercase strings N, NE, E, SE, S, SW, W, NW, meaning north, northeast, east, southeast, south, southwest, west, and northwest, respectively.

No two distinct launchers share the same coordinates, and for every ii, xix_i, yiy_i, and cic_i are integers.

Output

Print the minimum cost on the first line.

On the second line, print the indices of the launchers the marble passed through, including launcher ss and launcher ee. If there are multiple routes that achieve the minimum cost, print any of them.

If it is impossible to reach launcher ee, print −1-1 on the first line and terminate.

Hint

East is the direction in which the xx-coordinate increases, and north is the direction in which the yy-coordinate increases.

Examples2

  1. Example 1

    Input
    4 1 4
    1 5 1 E
    5 5 2 SE
    5 1 4 W
    1 1 3 N
    
    Expected output
    1
    1 3 4
    
  2. Example 2

    Input
    4 1 4
    1 4 4 S
    4 5 2 SW
    5 2 1 N
    2 1 3 W
    
    Expected output
    -1