spiderclimb

Spiders Climbing Trees

Xiao F has planted a tree with n nodes in his yard. The nodes are numbered 1 through n. There are n1 edges connecting these nodes, and the ith edge connects node ui to node vi, and has length wi.

Over the next few days, Xiao F makes m1 copies of this tree and arranges them in a row, so that he has m identical trees. The xth node of the kth tree node is numbered (k1)n+x. For convenience, each node numbered (k1)n+x will be denoted by the pair (k,x) below.

Xiao F also has q spiders in his yard. These spiders will connect nodes (k,x) to (k+1,x) with a thread of spider silk, for any 1k<m, 1xn.

Since each node has a different height and position, the lengths of thread between different nodes may differ. But since these trees are identical and the distances between neighboring trees are the same, for any 1k1,k2<m,1xn, the length of the spider silk connecting (k1,x) to (k1+1,x) is equal to the length of the spider silk connecting (k2,x) to (k2+1,x).

So we can describe these threads with a sequence a1,a2,,an, where ax denotes the lengths of the threads connecting (k,x) and (k+1,x) for any 1k<m.

To test the results, the spiders have a tree-climbing competition. The jth spider has to climb from node sj to node tj along the edges of the tree and on the spider silk threads.

Help the spiders to calculate each of their shortest path lengths.

Limits

1n,q2105, 1m109, 1ax109, 1wi1012, 1ui,vin,1sj,tjnm.

Subtask # Score Constraints
1 3 n100,m100,q1000
2 5 n,q5000
3 11 m20
4 12 The tree is a line graph.
5 9 The tree is a star graph.
6 22 sj=1
7 18 n,q5104
8 20 No additional constraints

Input format

The first line of input contains 3 integers n, m, and q, representing the number of nodes in each tree, the number of trees, and the number of spiders.

The second line of input contains n integers a1,a2,,an, the lengths of the spider silk threads.

The next n1 lines of input contain 3 integers each ui,vi,wi, describing an edge of the trees.

The next q lines of input data contain 2 integers each sj,tj, representing the starting and ending points of the jth spider.

Output format

Output q lines each containing a single integer dj - the shortest path length of the jth spider.

Samples

Sample Input 1 Sample Output 1
4 3 2
4 3 3 5
1 2 2
2 4 1
3 2 4
12 3 7 9
11
9


Submitting .cpp to 'spiderclimb'


You're not logged in! Click here to login

Time Limit: 8 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: LOJ #3661

Subtask Score
1 3
2 5
3 11
4 12
5 9
6 22
7 18
8 20
9 0