#P7528. [USACO21OPEN] Portals G

[USACO21OPEN] Portals G

Description

Bessie is in a network consisting of NN nodes labeled 1N1\dots N and 2N2N portals labeled 12N1\cdots 2N. Each portal connects two different nodes uu and vv (uvu \ne v). There may be multiple portals connecting the same pair of nodes.

Each node vv is connected to four distinct portals. The list of portals adjacent to vv is given by pv=[pv,1,pv,2,pv,3,pv,4]p_v=[p_{v,1},p_{v,2},p_{v,3},p_{v,4}].

Your current position can be represented by an ordered pair (current node, current portal), i.e. a pair (v,pv,i)(v,p_{v,i}), where 1vN1 \le v \le N and 1i41 \le i \le 4. You can change your position using either of the following operations:

    1. Change the current node by going through the current portal.
    1. Change the current portal. At each node, the first two portals in the list are paired, and the last two portals are also paired. That is, if your position is (v,pv,2)(v,p_{v,2}), you may switch to portal (v,pv,1)(v,p_{v,1}), and vice versa. Similarly, if your position is (v,pv,3)(v,p_{v,3}), you may switch to portal (v,pv,4)(v,p_{v,4}), and vice versa. There is no other way to change portals (for example, you cannot switch from portal pv,2p_{v,2} to portal pv,4p_{v,4}).

There are 4N4N distinct positions in total. Unfortunately, it is not necessarily true that every position can reach every other position through a sequence of operations. Therefore, for a cost of cvc_v mooney, you may reorder the list of portals adjacent to vv in any order. After that, the first two portals in the list are paired with each other, and the last two portals are paired with each other.

For example, if you reorder the portals adjacent to vv into [pv,3,pv,1,pv,2,pv,4][p_{v,3},p_{v,1},p_{v,2},p_{v,4}], this means that when you are at node vv,

  • If you are currently at portal pv,1p_{v,1}, you can switch to portal pv,3p_{v,3}, and vice versa.
  • If you are currently at portal pv,2p_{v,2}, you can switch to portal pv,4p_{v,4}, and vice versa. You can no longer switch from portal pv,1p_{v,1} to portal pv,2p_{v,2}, or from pv,3p_{v,3} to pv,4p_{v,4}, and vice versa.

Compute the minimum amount of mooney needed to modify the network so that every position can reach every other position. The input guarantees that there exists at least one valid way to modify the network.

Input Format

The first line contains NN.

The next NN lines each describe one node. Line v+1v+1 contains five space-separated integers cv,pv,1,pv,2,pv,3,pv,4c_v,p_{v,1},p_{v,2},p_{v,3},p_{v,4}.

The input guarantees that for each vv, pv,1,pv,2,pv,3,pv,4p_{v,1},p_{v,2},p_{v,3},p_{v,4} are all distinct, and each portal appears in the adjacency lists of exactly two nodes.

Output Format

Output one line containing the minimum amount of mooney needed to modify the network so that every position can reach every other position.

5
10 1 4 8 9
11 1 2 5 6
12 9 10 2 3
3 4 3 6 7
15 10 8 7 5
13

Hint

Sample Explanation

It is enough to reorder the adjacency lists of nodes 11 and 44. This costs a total of c1+c4=13c_1+c_4=13 mooney. We can make p1=[1,9,4,8]p_1=[1,9,4,8] and p4=[7,4,6,3]p_4=[7,4,6,3].

Constraints

2N1052 \le N \le 10^5, 1cv1031 \le c_v \le 10^3.

Translated by ChatGPT 5