QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#22453. Chocolate Giving

Statistics

Farmer John is distributing chocolates at the barn for Valentine's day, and $B$ ($1 \le B \le 25,000$) of his bulls have a special cow in mind to receive a chocolate gift.

Each of the bulls and cows is grazing alone in one of the farm's $N$ ($2*B \le N \le 50,000$) pastures conveniently numbered $1..N$ and connected by $M$ ($N-1 \le M \le 100,000$) bidirectional cowpaths of various lengths. Some pastures might be directly connected by more than one cowpath. Cowpath $i$ connects pastures $R_{i}$ and $S_{i}$ ($1 \le R_{i} \le N; 1 \le S_{i} \le N$) and has length $L_{i}$ ($1 \le L_{i} \le 2,000$).

Bull $i$ resides in pasture $P_{i}$ ($1 \le P_{i} \le N$) and wishes to give a chocolate to the cow in pasture $Q_{i}$ ($1 \le Q_{i} \le N$).

Help the bulls find the shortest path from their current pasture to the barn (which is located at pasture 1) and then onward to the pasture where their special cow is grazing. The barn connects, one way or another (potentially via other cowpaths and pastures) to every pasture.

As an example, consider a farm with 6 pastures, 6 paths, and 3 bulls (in pastures 2, 3, and 5) who wish to bestow chocolates on their love-objects:

                      *1  <-- Bull wants chocolates for pasture 1 cow
             [4]--3--[5]  <-- [5] is the pasture ID
            /  |
           /   |
          4    2          <-- 2 is the cowpath length
         /     |               between [3] and [4]
      [1]--1--[3]*6
     /   \    /
    9     3  2
   /       \/
 [6]      [2]*4
  • The Bull in pasture 2 can travel distance 3 (two different ways) to get to the barn then travel distance 2+1 to pastures $[3]$ and $[4]$ to gift his chocolate. That's 6 altogether.

  • The Bull in pasture 5 can travel to pasture 4 (distance 3), then pastures 3 and 1 (total: $3 + 2 + 1 = 6$) to bestow his chocolate offer.

  • The Bull in pasture 3 can travel distance 1 to pasture 1 and then take his chocolate 9 more to pasture 6, a total distance of 10.

Input

  • Line 1: Three space separated integers: $N$, $M$, and $B$

  • Lines $2..M+1$: Line $i+1$ describes cowpath $i$ with three space-separated integers: $R_{i}$, $S_{i}$, and $L_{i}$

  • Lines $M+2$..$M+B+1$: Line $M$+$i+1$ contains two space separated integers: $P_{i}$ and $Q_{i}$

Output

  • Lines $1..B$: Line $i$ should contain a single integer, the smallest distance that the bull in pasture $P_{i}$ must travel to get chocolates from the barn and then award them to the cow of his dreams in pasture $Q_{i}$

Examples

Input 1

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

Output 1

6
6
10

Problem credits: Brian Hamrick, 2010

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.