Job Hunt
InterviewTime limit1sMemory limit128 MB
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 () 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 dollars there. There is no limit on how many times she can do this.
Bessie's world has () cities, numbered through , connected by () one-way paths. Bessie is currently in city (). Path runs one-way from city to city (; ) and costs nothing to traverse.
To help Bessie, Farmer John gives her access to his private jet service. This service has () routes; each route is a one-way flight from a city to another city (; ) that costs () 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 dollars in every city she can reach, what is the most money she can make? Print if there is no limit to this amount.
Input
- Line 1: Five space-separated integers: , , , , and .
- Next lines: line contains two space-separated integers and describing a one-way path from one city to another.
- Next lines: each line contains three space-separated integers , , and 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 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 , and she can earn only dollars in each city before moving on.
Bessie can travel city city city city and make a total of dollars.