This page is still under construction.

Parts of this page are still being built. What you see may change.

Job Hunt

Interview

Time limit1sMemory limit128 MB

Summary
Bessie earns at most D per city visit, travels free paths and paid flights, and can repeat cities; find the maximum total profit or -1 if unbounded.
Level

Medium7 of 10

Topics
Graph, Shortest path, Dynamic programming, Greedy
Solved
No attempts yet

Problem

Bessie is running out of money and is looking for work. Farmer John knows this and wants his cows to travel around, so he has made a rule: a cow can earn at most DD (1≤D≤10001 \le D \le 1000) dollars in a city before she must go work in another city. However, after working elsewhere for a while, Bessie may return to a city and again earn up to DD dollars there. There is no limit on how many times she can do this.

Bessie's world has CC (2≤C≤2202 \le C \le 220) cities, numbered 11 through CC, connected by PP (1≤P≤1501 \le P \le 150) one-way paths. Bessie is currently in city SS (1≤S≤C1 \le S \le C). Path ii runs one-way from city AiA_i to city BiB_i (1≤Ai≤C1 \le A_i \le C; 1≤Bi≤C1 \le B_i \le C) and costs nothing to traverse.

To help Bessie, Farmer John gives her access to his private jet service. This service has FF (1≤F≤3501 \le F \le 350) routes; each route is a one-way flight from a city JiJ_i to another city KiK_i (1≤Ji≤C1 \le J_i \le C; 1≤Ki≤C1 \le K_i \le C) that costs TiT_i (1≤Ti≤500001 \le T_i \le 50000) dollars. Bessie may pay for tickets out of future earnings even if she has no cash on hand.

Bessie may retire whenever and wherever she likes. Given unlimited time, and assuming she earns the full DD dollars in every city she can reach, what is the most money she can make? Print −1-1 if there is no limit to this amount.

Input

  • Line 1: Five space-separated integers: DD, PP, CC, FF, and SS.
  • Next PP lines: line ii contains two space-separated integers AiA_i and BiB_i describing a one-way path from one city to another.
  • Next FF lines: each line contains three space-separated integers JiJ_i, KiK_i, and TiT_i describing a one-way jet flight from one city to another and its price.

Output

  • Line 1: A single integer, the most money Bessie can make while obeying the rule. Print −1-1 if there is no limit to this amount.

Hint

In this example the world has five cities, three paths, and two jet routes. Bessie starts in city 11, and she can earn only 100100 dollars in each city before moving on.

Bessie can travel city 1→1 \to city 5→5 \to city 2→2 \to city 33 and make a total of 4×100−150=2504 \times 100 - 150 = 250 dollars.

Examples1

  1. Example 1

    Input
    100 3 5 2 1
    1 5
    2 3
    1 4
    5 2 150
    2 5 120
    
    Expected output
    250