CF BUDDY
← Problems·

1433G · Reducing Delivery Cost

2100 · brute force, graphs, shortest paths

Problem: You are a mayor of Berlyatov. There are nn districts and mm two-way roads between them. The ii-th road connects districts xix_i and yiy_i. The cost of travelling along this road is wiw_i. There is some path between each pair of districts, so the city is connected.

There are kk delivery routes in Berlyatov. The ii-th route is going from the district aia_i to the district bib_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district aia_i to the district bib_i to deliver products.

The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).

You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 00).

Let d(x,y)d(x, y) be the cheapest cost of travel between districts xx and yy.

Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 00. In other words, you have to find the minimum possible value of i=1kd(ai,bi)\sum\limits_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.

Input Format: The first line of the input contains three integers nn, mm and kk (2n10002 \le n \le 1000; n1mmin(1000,n(n1)2)n - 1 \le m \le min(1000, \frac{n(n-1)}{2}); 1k10001 \le k \le 1000) — the number of districts, the number of roads and the number of courier routes.

The next mm lines describe roads. The ii-th road is given as three integers xix_i, yiy_i and wiw_i (1xi,yin1 \le x_i, y_i \le n; xiyix_i \ne y_i; 1wi10001 \le w_i \le 1000), where xix_i and yiy_i are districts the ii-th road connects and wiw_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.

The next kk lines describe courier routes. The ii-th route is given as two integers aia_i and bib_i (1ai,bin1 \le a_i, b_i \le n) — the districts of the ii-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).

Output Format: Print one integer — the minimum total courier routes cost you can achieve (i.e. the minimum value i=1kd(ai,bi)\sum\limits_{i=1}^{k} d(a_i, b_i), where d(x,y)d(x, y) is the cheapest cost of travel between districts xx and yy) if you can make some (at most one) road cost zero.

Note: The picture corresponding to the first example:

There, you can choose either the road (2,4)(2, 4) or the road (4,6)(4, 6). Both options lead to the total cost 2222.

The picture corresponding to the second example:

There, you can choose the road (3,4)(3, 4). This leads to the total cost 1313.

Sample Cases

Case 1

Input

6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3

Output

22

Case 2

Input

5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5

Output

13

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback