National Olympiad in Informatics, China, 2004
City is a famous tourist attraction in country . Under the leadership of Mayor , the people live and work peacefully in the thriving city. However, Mayor has not once been carried away by his achievements. His clear head is very aware that there are still some issues that exist in the governance of the city. One of these issues is traffic problems.
City has horizontal and vertical streets. The horizontal streets run east-west, and the vertical streets run north-south, forming the traffic network of city (as depicted below).
Due to the narrow streets, each street is only one-way. The direction of each street is predetermined. The direction of a horizontal street can only be either east or west. The direction of a vertical street can only be north or south. Going against the designated direction of a street is strictly prohibited.
This limitation is a major inconvenience for traffic. As can be seen in the above figure, tourists may, for example, wish to go from their hotel to the shopping center. However, due to the one-way streets, they have to travel in a big circle to reach their destination.
This problem has been puzzling Mayor for a while now. Every day, he will receive many letters from tourists, complaining about city 's unreasonable traffic system. Due to the large number of streets, he and his councilmen are still unable to solve this problem.
Fortunately, this problem can soon be resolved. This is because recently, he has paid good money to hire the world-renowned traffic engineer, Mr. , to renovate city 's traffic system for efficiency.
Mr. knows that the problem cannot be solved through expanding the width of the streets, since this will impact the various tourist attractions located along each side of the streets. So, he prepares to reassign the directions of some one-way streets to maximize the satisfaction of everyone.
Mr. assigned labels to each street in city . The horizontal streets are labeled with the numbers , in order from north to south. The vertical streets are labeled with the numbers , in order from west to east. This way, the position of any intersection can be represented using a pair of integers, where the first integer is the number of the horizontal street at that intersection and the second integer is the number of the vertical street at that intersection. This pair shall be known as the coordinates of this intersection. In the figure above, the hotel's coordinates would therefore be .
After a long period of research, he has assembled some requests made collectively by tourists. Each request can be written in the following form: the length of the shortest path from one intersection to another intersection must be equal to the Manhattan distance between the intersections. The Manhattan distance between two intersections refers to the sum of their horizontal and their vertical distances. Two intersections with coordinates and has a Manhattan distance of .
Mr. B already knows the directions of all the streets in city as well as the requests collectively made by the tourists. Can he reassign the directions of streets to fulfill all of the requests?
Additionally, changing the direction of each street requires a fixed working cost. The size of working costs depend on the streets. Mr. B not only wants to find a valid plan, he also wants the total working cost of this plan to be minimized. Can you help him?
- The first line of input contains two integers and , representing the number of horizontal and vertical streets, respectively.
- The second line of input contains a sequence of characters of length
, the directions of the horizontal streets before the
reassignments are made, in order from north to south.
Esignifies an eastbound street and
Wsignifies a westbound street.
- The third line of input contains a sequence of characters of length
, the directions of the vertical streets before the
reassignments are made, in order from west to east.
Nsignifies an northbound street and
Ssignifies a southbound street.
- The fourth line contains nonnegative integers, the working costs to change each of the horizontal streets, in order from north to south.
- The fourth line contains nonnegative integers, the working costs to change each of the vertical streets, in order from west to east.
- The sixth line contains a single positive integer , representing the number of requests made by the tourists.
- The next lines each contain four positive integers , , , and , each describing one request. A request indicates that the tourists wish for the length of the shortest path from intersection to intersection to equal the Manhattan distance between the two intersection.
The first line of output should contain one string, either
possible to indicate that
there is a way to reassign directions to streets such that all the
requests in the input are satisfied. Output
impossible to indicate
that no matter how directions are reassigned, there is no way to satisfy
all the requests.
If the first output is
possible, then the second line should contain
a single integer representing the minimum total working cost, the third
line should contain a string of length representing the revised
directions of the horizontal streets in order from north to south
E for east,
W for west), and the fourth line should contain
a string of length representing the revised directions of the
vertical streets in order from west to east (output
N for north,
- The working cost to change the direction of any street will not exceed .
2 3 WE NNS 3 9 1 4 2 2 1 3 2 1 2 3 2 2
possible 9 WW NNS
Your score out of 10 for each test case is determined as follows:
- If the first line of your program's output is
- If there is actually no solution, then you will score full points.
- If there is actually a solution, then you will score 0 points.
- If the first line of your program's output is
- If the remaining lines are incorrectly formatted, your strategy is infeasible, or your total working cost is incorrect in relation to your strategy, then you will score 0 points.
- If your strategy is feasible, but total working cost is not optimal, then you will score 4 points.
- If your strategy is both feasible and optimal, then you will score full points.
- Otherwise, your score will be 0 points.