#BF0000022. Tìm việc làm (Job Hunt)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tìm việc làm (Job Hunt)
Job Hunt
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
Bessie starts in city among cities. She earns dollars whenever she works in a city, including the starting city, and may earn again after leaving and returning. There are free one-way paths and one-way flights costing . Flights may be paid from future income, so intermediate cash need not be nonnegative. Bessie may retire in any city at any time. Find her maximum possible money, or print -1 if it is unlimited.
Input
The first line contains . Then follow lines for free paths and lines for flights.
Output
Print the maximum achievable money, or -1 if unbounded.
Subtasks
-
Subtask 1 (20 points): ; ; ; ; ; routes are directed and connect distinct cities.
-
Subtask 2 (30 points): ; ; ; ; ; routes are directed and connect distinct cities.
-
Subtask 3 (50 points): ; ; ; ; ; routes are directed and connect distinct cities.
Examples
Input
100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120
Output
250
Explanation
Work in city , take the free path to , fly to for , then take the free path to : .