The Travelling Salesman

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Author:
Problem types

Not actually The Travelling Salesman problem
A travelling salesman is visiting different cities selling his wares and making a living. These cities are located on a grid. This grid increases positively along the x-axis towards the right and positively along the y-axis downwards. At each city, the salesman makes a certain amount of coins in sales. Can you calculate his total earnings after the salesman travels a certain distance?

Input Specification

The first line of input contains 2 space separated integers, x, y (1 \le x, y \le 1000) representing the salesman's starting coordinates.
The next line of input contains n (1 \le n \le 100\,000) representing the number of cities on the grid.
The next n lines contain 3 space separated integers, first cx_i, cy_i (1 \le cx_i, cy_i \le 1000) representing the x and y coordinates of each city followed by s_i (1 \le s_i \le 100) representing the money the salesman would earn should he visit this city.
The next line contains k (1 \le k \le 100\,000) representing the total number of squares the salesman travels.
The next k lines each represent a direction in which the salesman traveled. The salesman traveled 1 square in each of these directions in order of when they appear. U represents upwards travel, D represents downward travel, L represents leftward travel, and R represents rightward travel. It is possible for the salesman to visit the same city more than once, in this case, he will earn the same amount of coins again. It is guaranteed that the salesman's current x and y position will always remain (1 \le x, y \le 1000).

Output Specification

Print how much money the salesman would earn if he were to adhere to this given route.

Sample Input

1 1
3
4 2 7
3 3 12
10 8 6
6
D
D
R
R
U
R

Sample Output

19

Explanation for Sample

The salesman starts at position (1, 1). There are 3 cities located at (4, 2), (3, 3), (10, 8) with 7, 12 and 6 coins to be earned at each respectively. The salesman travels 6 squares as follows:
D - The salesman moves to (1, 2).
D - The salesman moves to (1, 3).
R - The salesman moves to (2, 3).
R - The salesman moves to (3, 3) and collects 12 coins from the city there.
U - The salesman moves to (3, 2).
R - The salesman moves to (4, 2) and collects 7 coins from the city there.
The salesman never visits the city at (10, 8) and collects a total of 19 coins.


Comments

There are no comments at the moment.