Sunny Island
Time limit1sMemory limit128 MB
Assign product sets of given minimum sizes to stalls on a ring, non-crossing bridges, and rays so neighbours share nothing, minimizing the product count.
- Level
Medium5 of 10
- Topics
- Greedy, Graph, Implementation
- Solved
- No attempts yet
Problem
One day a volcano erupted in the middle of the Pacific. After the lava cooled, a new island emerged shaped like a little sun: the land above the water formed a circle (the crater), and the lava that flowed out solidified into straight rays pointing away from the circle.
Tourists soon arrived and market stalls were set up. Stalls stand in three kinds of places.
- Around the crater rim, stalls are numbered to clockwise. Consecutively numbered stalls are neighbours, and stall neighbours stall , so the rim stalls form a ring.
- Bridges are built across the crater, each resting on two rim stalls. No two bridges cross or lie above one another. The two stalls joined by a bridge are neighbours.
- Rays of solidified lava reach outward from certain rim stalls. Along a ray the stalls form a chain: the first ray stall neighbours the rim stall it starts from, and each following stall neighbours the previous one.
A local law forbids two neighbouring stalls from sharing even a single product, so neighbours must sell completely different assortments. Each stall must sell at least a given number of distinct products to stay in business (no stalls may be placed on the bridges).
Determine the smallest number of different products that must be shipped to the island so that every stall can be given at least its required number of products while no two neighbouring stalls share any product.
Input
The first line contains the number of datasets (). Each dataset is given as follows.
The first line contains (), the number of stalls on the crater rim, numbered to clockwise.
The next line contains (), the number of bridges. Each of the following lines contains two integers and (, , ): the rim stalls at the two ends of a bridge.
The next line contains (), the number of lava rays. Each of the following lines contains two integers and (, ): the rim stall from which ray starts and the number of additional stalls on that ray.
The next line contains integers; the -th is the number of distinct products the rim stall numbered must sell.
Finally there are lines; the -th contains integers giving the required product counts of the successive stalls on ray , ordered from the stall nearest the crater to the farthest.
There are at most stalls in total, and no stall requires more than products.
Output
For each dataset, print a single line with the minimum number of different products that must be shipped to the island so that every stall receives at least its required number of products and no two neighbouring (or bridge-joined) stalls share any product.
Hint

Figure 1. Map of the island described in the example.