CF1140G.Double Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a special undirected graph. It consists of 2n2n vertices numbered from 11 to 2n2n . The following properties hold for the graph:

  • there are exactly 3n23n-2 edges in the graph: nn edges connect vertices having odd numbers with vertices having even numbers, n1n - 1 edges connect vertices having odd numbers with each other, and n1n - 1 edges connect vertices having even numbers with each other;
  • for each edge (u,v)(u, v) between a pair of vertices with odd numbers, there exists an edge (u+1,v+1)(u + 1, v + 1) , and vice versa;
  • for each odd number u[1,2n1]u \in [1, 2n - 1] , there exists an edge (u,u+1)(u, u + 1) ;
  • the graph is connected; moreover, if we delete all vertices with even numbers from it, and all edges incident to them, the graph will become a tree (the same applies to deleting odd vertices).

So, the graph can be represented as two trees having the same structure, and nn edges connecting each vertex of the first tree to the corresponding vertex of the second tree.

Edges of the graph are weighted. The length of some simple path in the graph is the sum of weights of traversed edges.

You are given qq queries to this graph; in each query, you are asked to compute the length of the shortest path between some pair of vertices in this graph. Can you answer all of the queries?

输入格式

The first line of the input contains one integer nn ( 2n31052 \le n \le 3 \cdot 10^5 ).

The second line contains nn integers w1,2w_{1, 2} , w3,4w_{3,4} , ..., w2n1,2nw_{2n - 1, 2n} ( 1wi,i+110121 \le w_{i, i + 1} \le 10^{12} ). These integers describe the weights of the edges connecting odd vertices with even ones.

Then n1n-1 lines follow. ii -th line contains four integers xix_i , yiy_i , wi,1w_{i, 1} and wi,2w_{i, 2} ( 1xi,yin1 \le x_i, y_i \le n , xiyix_i \ne y_i , 1wi,j10121 \le w_{i, j} \le 10^{12} ); it describes two edges: one connecting 2xi12x_i - 1 with 2yi12y_i - 1 and having weight wi,1w_{i, 1} ; another connecting 2xi2x_i with 2yi2y_i and having weight wi,2w_{i, 2} .

The next line contains one integer qq ( 1q61051 \le q \le 6 \cdot 10^5 ) — the number of queries.

Then qq lines follow, ii -th line contains two integers uiu_i and viv_i ( 1ui,vi2n1 \le u_i, v_i \le 2n , uiviu_i \ne v_i ), describing a query "compute the length of the shortest path between vertices uiu_i and viv_i ".

输出格式

Print qq integers, ii -th integer should be equal to the answer to the ii -th query.

输入输出样例

  • 输入#1

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

    输出#1

    3
    15
    4
    

说明/提示

The graph in the first test looks like that:

首页