You are on page 1of 2

An important event is happening today in your city, and there are many tourists

lost. To get to their destinations in this unknown city they usually order cabs.
You have been hired to help the cab drivers, who are overwhelmed with so many o
rders.
The city can be represented with N reference points, and M roads that connect su
ch points. Each road has a length, and there will always exist a path between an
y two distinct points of the city.
The cab driver goal is to answer to K orders. Each order consists of two points
in the city, O and D (source and destiny), where the tourist is currently at the
point O and wants to get to the point D. The cab driver is initially in the poi
nt 1, he intends to answer only one order at each time (in any order he wants),
and after the last order he must return to the point 1.
For example, consider a city with N = 5 reference points and K = 2 tourists, whe
re the first tourist wants to get from point 4 to point 3, and the second wants
to get from point 2 to point 4. Therefore, the cab driver has two possible route
s: 1 -> 4 -> 3 -> 2 -> 4 -> 1; or 1 -> 2 -> 4 -> 3 -> 1. Notice that A -> B repr
esents a path between the points A and B, which consists of one or more roads.
Puzzled with so many route options and wanting to save fuel, the cab driver aske
d you to calculate which route has the minimum travelled distance.
Input
Each test case starts with three integers N, M and K (2 ? N ? 104, N-1 ? M ? 105
, 1 ? K ? 15).
Following there will be M rows, each with three integers A, B and C each, repres
enting that there is a road that connects the points A and B, which can be trave
lled in both directions, with length C (1 ? A, B ? N, 1 ? C ? 100, A <> B).
Following there will be K rows, each with two integers O and D each, representin
g that there is a tourist who wants to get from point O to point D (1 ? O, D ? N
, O <> D).
Output
For each test case you should print one line containing one integer, representin
g the minimum possible distance to be travelled if the cab driver starts at poin
t 1, answer all the orders (one at a time), and come back to point 1.
Input Samples
3
1
2
3
2

3
2
3
1
3

1
2
4
3

5
1
1
1

7
2
3
4

3
3
7
5

Output Samples
9

26

2
3
3
4
2
4
1

3
4
5
5
4
5
3

2
6
5
3

You might also like