#abc270f. F - Transportation

F - Transportation

Score : 500500 points

问题描述

在AtCoder共和国,有NN个岛屿。最初,没有任何一个岛屿拥有机场或港口,并且任意两个岛屿之间都没有道路相连。国王Takahashi计划为这些岛屿提供某种交通方式。具体来说,他可以按照任意顺序进行任意次数的以下操作:

  • 选择一个整数ii,满足1iN1\leq i\leq N,并支付XiX_i的成本在第ii个岛屿上建设机场。
  • 选择一个整数ii,满足1iN1\leq i\leq N,并支付YiY_i的成本在第ii个岛屿上建设港口。
  • 选择一个整数ii,满足1iM1\leq i\leq M,并支付ZiZ_i的成本建设一条双向连接第AiA_i个岛屿和第BiB_i个岛屿的道路。

Takahashi的目标是确保对于每一对不同的岛屿UUVV,当可以按照任意顺序无限次执行以下动作时,可以从岛屿UU到达岛屿VV

  • 当岛屿SSTT都有机场时,可以从岛屿SS前往岛屿TT
  • 当岛屿SSTT都有港口时,可以从岛屿SS前往岛屿TT
  • 当岛屿SSTT通过道路相连时,可以从岛屿SS前往岛屿TT

请找出Takahashi需要支付的最小总成本以实现他的目标。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

The Republic of AtCoder has NN islands. Initially, none of the islands has an airport or harbor, and there is no road between any two of them. Takahashi, the king, will provide some means of transportation between these islands. Specifically, he can do the following operations any number of times in any order.

  • Choose an integer ii such that 1iN1\leq i\leq N and pay a cost of XiX_i to build an airport on island ii.
  • Choose an integer ii such that 1iN1\leq i\leq N and pay a cost of YiY_i to build a harbor on island ii.
  • Choose an integer ii such that 1iM1\leq i\leq M and pay a cost of ZiZ_i to build a road that bidirectionally connects island AiA_i and island BiB_i.

Takahashi's objective is to make it possible, for every pair of different islands UU and VV, to reach island VV from island UU when one can perform the following actions any number of times in any order.

  • When islands SS and TT both have airports, go from island SS to island TT.
  • When islands SS and TT both have harbors, go from island SS to island TT.
  • When islands SS and TT are connected by a road, go from island SS to island TT.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1M2×1051 \leq M \leq 2\times 10^5
  • 1Xi1091\leq X_i\leq 10^9
  • 1Yi1091\leq Y_i\leq 10^9
  • 1Ai<BiN1\leq A_i<B_i\leq N
  • 1Zi1091\leq Z_i\leq 10^9
  • (Ai,Bi)(Aj,Bj)(A_i,B_i)\neq (A_j,B_j), if iji\neq j.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

X1X_1 X2X_2 \ldots XNX_N

Y1Y_1 Y2Y_2 \ldots YNY_N

A1A_1 B1B_1 Z1Z_1

A2A_2 B2B_2 Z2Z_2

\vdots

AMA_M BMB_M ZMZ_M

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective.

Sample Input 1

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6

Sample Output 1

16

Takahashi will build the following infrastructure.

  • Pay a cost of X1=1X_1=1 to build an airport on island 11.
  • Pay a cost of X3=4X_3=4 to build an airport on island 33.
  • Pay a cost of Y2=2Y_2=2 to build a harbor on island 22.
  • Pay a cost of Y4=3Y_4=3 to build a harbor on island 44.
  • Pay a cost of Z2=6Z_2=6 to build a road connecting island 11 and island 44.

Then, the objective will be achieved at a cost of 1616. There is no way to achieve the objective for a cost of 1515 or less, so 1616 should be printed.

Sample Input 2

3 1
1 1 1
10 10 10
1 2 100

Sample Output 2

3

It is not mandatory to build all three types of facilities at least once.

Sample Input 3

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21

Sample Output 3

160

update @ 2024/3/10 11:24:10