atcoder#ABC245G. [ABC245G] Foreign Friends

[ABC245G] Foreign Friends

Score : 600600 points

Problem Statement

There are NN people and KK nations, labeled as Person 11, Person 22, \ldots, Person NN and Nation 11, Nation 22, \ldots, Nation KK, respectively. Each person belongs to exactly one nation: Person ii belongs to Nation AiA_i. Additionally, there are LL popular people among them: Person B1B_1, Person B2B_2, \ldots, Person BLB_L are popular. Initially, no two of the NN people are friends.

For MM pairs of people, Takahashi, a God, can make them friends by paying a certain cost: for each 1iM1\leq i\leq M, he can pay the cost of CiC_i to make Person UiU_i and Person ViV_i friends.

Now, for each 1iN1\leq i\leq N, solve the following problem.

Can Takahashi make Person ii an indirect friend of a popular person belonging to a nation different from that of Person ii? If he can do so, find the minimum total cost needed. Here, Person ss is said to be an indirect friend of Person tt when there exists a non-negative integer nn and a sequence of people (u0,u1,,un)(u_0, u_1, \ldots, u_n) such that u0=su_0=s, un=tu_n=t, and Person uiu_i and Person ui+1u_{i+1} are friends for each 0i<n0\leq i < n.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1M1051 \leq M \leq 10^5
  • 1K1051 \leq K \leq 10^5
  • 1LN1 \leq L \leq N
  • 1AiK1 \leq A_i \leq K
  • $1 \leq B_1
  • 1Ci1091\leq C_i\leq 10^9
  • $1\leq U_i
  • (Ui,Vi)(Uj,Vj)(U_i, V_i)\neq (U_j,V_j) if iji \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM KK LL

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BLB_L

U1U_1 V1V_1 C1C_1

U2U_2 V2V_2 C2C_2

\vdots

UMU_M VMV_M CMC_M

Output

Let XiX_i defined as follows: XiX_i is 1-1 if it is impossible to make Person ii an indirect friend of a popular person belonging to a nation different from that of Person ii; otherwise, XiX_i is the minimum total cost needed to do so. Print X1,X2,,XNX_1, X_2, \ldots, X_N in one line, with spaces in between.

4 4 2 2
1 1 2 2
2 3
1 2 15
2 3 30
3 4 40
1 4 10
45 30 30 25

Person 11, 22, 33, 44 belong to Nation 11, 11, 22, 22, respectively, and there are two popular people: Person 22 and 33. Here,

  • For Person 11, the only popular person belonging to a different nation is Person 33. To make them indirect friends with the minimum cost, we should pay the cost of 1515 to make Person 11 and 22 friends and pay 3030 to make Person 22 and 33 friends, for a total of 15+30=4515+30=45.
  • For Person 22, the only popular person belonging to a different nation is Person 33. The minimum cost is achieved by making Person 22 and 33 friends by paying 3030.
  • For Person 33, the only popular person belonging to a different nation is Person 22. The minimum cost is achieved by making Person 22 and 33 friends by paying 3030.
  • For Person 44, the only popular person belonging to a different nation is Person 22. To make them indirect friends with the minimum cost, we should pay the cost of 1515 to make Person 11 and 22 friends and pay 1010 to make Person 11 and 44 friends, for a total of 15+10=2515+10=25.
3 1 3 1
1 2 3
1
1 2 1000000000
-1 1000000000 -1

Note that, for Person 11, Person 11 itself is indeed an indirect friend, but it does not belong to a different nation, so there is no popular person belonging to a different nation.