#P1938. [USACO09NOV] Job Hunt S

[USACO09NOV] Job Hunt S

Description

Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D(1D1,000)D (1 \le D \le 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.

Bessie's world comprises P(1P150)P (1 \le P \le 150) one-way paths connecting C(2C220)C (2 \le C \le 220) cities conveniently numbered 1C1 \sim C. Bessie is currently in city S(1SC)S (1 \le S \le C). Path i runs one-way from city AiA_i to city Bi(1AiC;1BiC)B_i (1 \le A_i \le C; 1 \le B_i \le C) and costs nothing to traverse.

To help Bessie, Farmer John will give her access to his private jet service. This service features F(1F350)F (1 \le F \le 350) routes, each of which is a one way flight from one city JiJ_i to a another Ki(1JiC;1KiC)K_i (1 \le J_i \le C; 1 \le K_i \le C) and which costs Ti(1Ti50,000)T_i (1 \le T_i \le 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn't have the cash on hand.

Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full DD dollars in each city she can travel to? Print 1-1 if there is no limit to this amount.

Input Format

The input is given from Standard Input in the following format:

D P C F S
A_1 B_1
A_2 B_2
...
A_n B_n
J_1 K_1 T_1
J_2 K_2 T_2
...
J_n K_n T_n

## Output Format

One integer representing the answer.

```input1
100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120

250

Hint

This world has five cities, three paths and two jet routes. Bessie starts out in city 11, and she can only make 100100 dollars in each city before moving on.

Bessie can travel from city 11 to city 55 to city 22 to city 33, and make a total of 4×100150=2504 \times 100 - 150 = 250 dollars.

Source: USACO 2009 November Silver