This page is still under construction.

Parts of this page are still being built. What you see may change.

Chocolate Giving

Time limit1sMemory limit128 MB

Summary
For each of B queries on a weighted undirected graph, output the shortest distance from pasture P to pasture Q that passes through the barn at pasture 1.
Level

Medium5 of 10

Topics
Graph, Shortest path, Heap, Implementation
Solved
No attempts yet

Problem

The farmer is handing out chocolates at the barn for Valentine's Day. BB (1≤B≤250001 \le B \le 25000) bulls each have a special cow in mind to receive a chocolate gift.

Each bull and cow grazes alone in one of the farm's NN (2B≤N≤500002B \le N \le 50000) pastures, numbered 11 through NN and connected by MM (N−1≤M≤100000N-1 \le M \le 100000) bidirectional cowpaths of various lengths. Two pastures may be directly connected by more than one cowpath. Cowpath ii connects pastures RiR_i and SiS_i (1≤Ri≤N1 \le R_i \le N; 1≤Si≤N1 \le S_i \le N) and has length LiL_i (1≤Li≤20001 \le L_i \le 2000).

Bull ii lives in pasture PiP_i (1≤Pi≤N1 \le P_i \le N) and wants to give a chocolate to the cow in pasture QiQ_i (1≤Qi≤N1 \le Q_i \le N).

Help each bull find the shortest route from its own pasture to the barn (located at pasture 11) and then onward to the pasture where its special cow grazes. The barn is connected -- directly or indirectly -- to every pasture.

For example, consider a farm with 6 pastures, 7 cowpaths, and 3 bulls (in pastures 2, 3, and 5) who each want to deliver a chocolate:

                     *1  <-- this bull wants to gift the cow in pasture 1
             [4]--3--[5]  <-- [5] is the pasture ID
            /  |
           /   |
          4    2          <-- 2 is the length of the cowpath
         /     |               between [3] and [4]
      [1]--1--[3]*6
     /   \    /
    9     3  2
   /       \/
 [6]      [2]*4

The bull in pasture 2 can travel distance 3 to reach the barn, then distance 2 + 1 to pastures 3 and 4, for a total of 6.

The bull in pasture 5 can travel to pasture 4 (distance 3), then on to pastures 3 and 1 (3 + 2 + 1 = 6).

The bull in pasture 3 can travel distance 1 to pasture 1, then carry the chocolate 9 more to pasture 6, for a total distance of 10.

Input

  • Line 1: three space-separated integers NN, MM, and BB.
  • Lines 2 to M+1M+1: line i+1i+1 describes cowpath ii with three space-separated integers RiR_i, SiS_i, and LiL_i.
  • Lines M+2M+2 to M+B+1M+B+1: line M+i+1M+i+1 contains two space-separated integers PiP_i and QiQ_i.

Output

  • Lines 1 to BB: line ii contains a single integer, the smallest total distance the bull in pasture PiP_i must travel to pick up a chocolate at the barn and then deliver it to the cow of his dreams in pasture QiQ_i.

Examples1

  1. Example 1

    Input
    6 7 3
    1 2 3
    5 4 3
    3 1 1
    6 1 9
    3 4 2
    1 4 4
    3 2 2
    2 4
    5 1
    3 6
    
    Expected output
    6
    6
    10