Log inSign up
Problems / A027

Waypoints

400 ptsCases 23Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

A student starts out standing at point 1 and wants to move to point N. The goal is to leave the starting point, point 1, and arrive at the destination, point N, as fast as possible. A passage may exist between any two nodes (points), and each passage comes with a passage length from 1 to 10. For example, if a passage has length 5, then moving through that passage means covering a distance of 5. There can be more than one passage joining the same two nodes.

The total number of nodes N is at most 8,000, and the number of passages M is at most 100,000. Passages are also two-way, so if a passage connects A and B then you can go from A to B and from B to A.

On the way, 3 waypoints must always be visited. The numbers of the 3 given waypoints are never 1 or N. The 3 waypoint numbers are also always different from each other. In other words, the student must travel from node 1 to node N by the shortest path that visits each of the 3 waypoints at least once. Write a program that computes the shortest distance for moving from node 1 to node N. If it is impossible to reach node N from node 1 by way of the 3 waypoints, return -1.

As an example, suppose N=7, M=10, and the waypoint nodes to visit are 2, 3 and 4. In that case, on the way from node 1 to node 7, nodes 2, 3 and 4 each have to be visited at least once. All the nodes and all the passages (edges) of this example are drawn in the figure below. The number written on a passage in the figure is the length of that passage. In the figure the waypoint nodes to visit are shaded red, and the start node 1 and the destination node N are shaded blue.

> (The graph figure from the original document was lost during text extraction. The edge information is the same as the edges array in the input and output examples.)

In this case, moving in the order 1 → 4 → 2 → 3 → 7 gives a total distance of 23 (7 + 5 + 6 + 5), and that value is the shortest distance meeting the problem's requirements.

> (The graph figure from the original document was lost during text extraction.)

Write a program that computes the shortest distance for going from node 1 to node N while visiting the 3 given points. In the input data N always has a value of 5 or more. The 3 waypoint nodes do not overlap each other, and they are never node 1 or node N.

While moving, the student may also visit the same node several times and pass through the same passage several times. The student must always move along a shortest path, the first node must be node 1, and the last node must be node N.

Input and output examples

Example 1

Input

7, 10, [[1, 2, 8], [1, 4, 7], [2, 3, 6], [2, 4, 5], [3, 5, 8], [3, 7, 5], [4, 5, 9], [4, 6, 8], [5, 6, 2], [6, 7, 8]], [2, 3, 4]

Output

23

Example 2

Input

5, 7, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2], [3, 4, 1], [3, 5, 5], [4, 5, 2]], [2, 3, 4]

Output

5

Example 3

Input

5, 5, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [3, 5, 5], [4, 5, 2]], [2, 3, 4]

Output

11

Example 4

Input

5, 5, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2], [3, 5, 5]], [2, 3, 4]

Output

11

Example 5

Input

5, 4, [[1, 2, 1], [1, 3, 3], [2, 4, 2], [3, 5, 5]], [2, 3, 4]

Output

14

Example 6

Input

5, 4, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [3, 5, 5]], [2, 3, 4]

Output

-1

Example 7

Input

5, 4, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2]], [2, 3, 4]

Output

-1

Input

  • First the number of nodes N is given. The value of N is a natural number from 5 to 8,000.

  • Next the number of passages M is given. The value of M is a natural number of at most 100,000. A passage means a two-way passage. So if A and B are connected, you can go from A to B and you can also go from B to A.

  • Next a two-dimensional array edges is given, holding the information about each passage. It is given as an M×3 array, and the information about each passage consists of A, B, K. This means A and B are connected, and K is the length of the passage joining A and B (the distance to cover when moving through the passage). A and B are different nodes, and the passage length is given as a natural number from 1 to 10.

  • Next a one-dimensional array points is given, holding the waypoint node information. It is a one-dimensional array with 3 elements, of the form [X, Y, Z]. The value of each of the three elements is a natural number greater than 1 and less than N, and the three values are different from each other.

Output

  • Return, as a natural number, the shortest distance for starting at node 1, visiting each of the 3 given waypoints at least once, and arriving at node N. If it is impossible to reach node N from node 1 by way of the 3 waypoints, return -1.

Requirements

  • You must write a solution(N, M, edges, points) function and return the answer
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 23 cases, and 8 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 23 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

8 public cases. Judging also uses hidden cases. You have to pass all 23 cases to solve this.

Input
Expected output
7, 10, [[1, 2, 8], [1, 4, 7], [2, 3, 6], [2, 4, 5], [3, 5, 8], [3, 7, 5], [4, 5, 9], [4, 6, 8], [5, 6, 2], [6, 7, 8]], [2, 3, 4]
23
5, 7, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2], [3, 4, 1], [3, 5, 5], [4, 5, 2]], [2, 3, 4]
5
5, 5, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [3, 5, 5], [4, 5, 2]], [2, 3, 4]
11
5, 5, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2], [3, 5, 5]], [2, 3, 4]
11
5, 4, [[1, 2, 1], [1, 3, 3], [2, 4, 2], [3, 5, 5]], [2, 3, 4]
14
5, 4, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [3, 5, 5]], [2, 3, 4]
-1
5, 4, [[1, 2, 1], [1, 3, 3], [2, 3, 1], [2, 4, 2]], [2, 3, 4]
-1
16, 3085, [[15, 6, 3], [1, 5, 4], [15, 16, 3], [15, 9, 10], [9, 10, 3], [14, 12, 2], [7, 1, 10], [12, 15, 6], [16, 2, 1], [9, 13, 2], [8, 14, 10], [9, 15, 2], [14, 3, 1], [10, 3, 2], [5, 16, 5], [12, 11, 4], [3, 7, 10], [10, 9, 1], [13, 3, 2], [5, 6, 2], [4, 9, 2], [4, 10, 10], [2, 15, 8], [2, 13, 4], [11, 15, 10], [9, 1, 1], [6, 12, 5], [14, 9, 3], [4, 15, 1], [13, 4, 1], [4, 3, 1], [12, 16, 9], [7, 2, 7], [2, 3, 7], [5, 13, 2], [9, 2, 9], [3, 2, 2], [1, 8, 7], [1, 10, 4], [16, 6, 2], [2, 6, 4], [14, 6, 10], [3, 16, 10], [12, 14, 8], [2, 6, 2], [9, 5, 1], [8, 12, 7], [2, 3, 9], [16, 6, 2], [12, 5, 6], [11, 2, 2], [12, 6, 5], [1, 15, 4], [13, 1, 4], [13, 1, 9], [3, 11, 6], [6, 16, 7], [6, 8, 2], [4, 11, 4], [7, 8, 6], [4, 9, 10], [5, 6, 8], [9, 8, 8], [1, 13, 2], [4, 11, 4], [1, 10, 3], [3, 4, 5], [2, 8, 1], [6, 9, 8], [8, 5, 3], [10, 13, 9], [14, 13, 6], [4, 16, 2], [15, 4, 5], [2, 13, 5], [13, 5, 9], [9, 13, 8], [8, 5, 1], [16, 10, 10], [7, 15, 5], [1, 15, 10], [4, 3, 7], [9, 12, 10], [16, 1, 9], [9, 15, 3], [9, 16, 3], [7, 9, 4], [15, 14, 6], [1, 6, 5], [15, 4, 7], [12, 7, 7], [9, 13, 1], [4, 14, 3], [12, 3, 6], [3, 10, 4], [5, 13, 7], [14, 2, 6], [9, 12, 6], [8, 2, 1], [4, 9, 5

... cut off here (36,958 chars in total)
4

Notes

Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.

This is problem 3 of practice test 9 in the original.

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 131ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 8 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal22.81s913 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Input read time 482ms (excluded from the ranking)

Log in and you can see the code once you solve this problem.