#P5766. [NOI1999] 最优联通子集

[NOI1999] 最优联通子集

Description

As is well known, we can use the Cartesian coordinate system to uniquely represent any point PP on the plane by an ordered pair (x,y)(x,y). If both x,yx,y are integers, then we call point PP a lattice point; otherwise, point PP is a non-lattice point. Let the set of all lattice points on the plane be denoted by WW.

Definition 1: For two lattice points P1(x1,y1),P2(x2,y2)P_1(x_1,y_1),P_2(x_2,y_2), if x1x2+y1y2=1|x_1-x_2|+|y_1-y_2|=1, then P1P_1 and P2P_2 are said to be adjacent, denoted by P1P_1~P2P_2. Otherwise, they are said to be non-adjacent.

Definition 2: Let a point set SS be a finite subset of WW, that is, S={P1,P2,,Pn}S=\{P_1,P_2,\ldots,P_n\} (n1)(n \ge 1), where PiP_i (1in)(1 \le i \le n) belongs to WW. We call SS a lattice point set.

Definition 3: Let SS be a lattice point set. If points RR and TT belong to SS, and there exists a finite point sequence Q1,Q2,,QkQ_1,Q_2,\ldots,Q_k satisfying:

  1. QiQ_i belongs to SS (1ik1 \le i \le k);
  2. Q1=RQ_1=R, Qk=TQ_k=T;
  3. QiQ_i~Qi+1Q_{i+1} (1ik1)(1 \le i \le k-1), i.e. QiQ_i and Qi+1Q_{i+1} are adjacent;
  4. For any 1i<jk1 \le i<j \le k, QiQjQ_i \ne Q_j;

then we say point RR and point TT are connected in the lattice point set SS. The point sequence Q1,Q2,,QkQ_1,Q_2,\ldots,Q_k is called a path in SS connecting RR and TT.

Definition 4: If a lattice point set VV satisfies that for any two lattice points in VV, there exists one and only one path in VV connecting these two points, then VV is called a single lattice point set.

Definition 5: For each lattice point on the plane, we can assign it an integer as the weight of that point. The sum of the weights of all points in a lattice point set is called the weight sum of that set.

For a given single lattice point set VV, we want to find an optimal connected subset BB of VV such that:

  1. BB is a subset of VV.
  2. Any two lattice points in BB are connected within BB.
  3. Among all lattice point sets satisfying conditions (1) and (2), BB has the maximum weight sum.

Input Format

The first line contains an integer NN, indicating the number of points in the single lattice point set VV.

In the next NN lines, the ii-th line (1iN)(1 \le i \le N) contains three integers Xi,Yi,CiX_i,Y_i,C_i, which represent the xx-coordinate, yy-coordinate, and weight of the ii-th point, respectively. Adjacent numbers on the same line are separated by one space.

Output Format

Output one integer, the weight sum of the required optimal connected set.

5
0 0 -2
0 1 1
1 0 1
0 -1 1
-1 0 1

2

Hint

Translated by ChatGPT 5