#P6768. [USACO05MAR] Ombrophobic Bovines 发抖的牛

[USACO05MAR] Ombrophobic Bovines 发抖的牛

Description

FJ’s cows are very afraid of getting rained on, which makes them shiver. They plan to install a rain alarm and arrange an evacuation plan. They need to compute the minimum time needed to get all cows into shelters.

The cows graze on FF fields on the farm. There are PP bidirectional roads connecting these fields. The roads are wide, so an unlimited number of cows can pass through them. Each field has a shelter with a certain capacity, and cows can enter the shelter on the same field instantly.

Compute the minimum time so that every cow can enter a shelter.

Input Format

Line 11: two integers FF and PP.
Lines 22 to F+1F+1: on line i+1i+1, there are two integers describing field ii. The first is the number of cows on the field, and the second is the shelter capacity on that field. Both integers are between 00 and 10001000.
Lines F+2F+2 to F+P+1F+P+1: each line contains three integers describing a road: the two endpoints, and the time needed to travel along this road (between 11 and 10910^9).

Output Format

One integer, the minimum time. If it is impossible to get all cows into shelters, output 1-1.

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

110

Hint

Constraints: for 100%100\% of the testdata, 1F2001 \le F \le 200, 1P15001 \le P \le 1500.

Translated by ChatGPT 5