National Olympiad in Informatics, China, 1999

Day 2, Problem 2 - Optimally Connected Subset

It is well-known that we can uniquely represent any point P on the Cartesian coordinate system using an ordered pair (x, y). If both x and y are integers, then we shall call P an integer point, otherwise we shall call P a non-integer point. We shall denote all integer points on the plane using the set W.

Definition 1: For two points P1(x1, y1), P2(x2, y2), if |x1x2| + |y1y2| = 1, then P1 and P2 shall be considered neighbors, which is denoted as P1 ~ P2. Otherwise, P1 and P2 are considered non-neighboring.

Definition 2: The set S is a finite subset of W such that S = {P1, P2, …, Pn} (n ≥ 1), where Pi (1 ≤ in) belongs in W. We shall call S a set of integer points.

Definition 3: Where S is a set of integer points, if the points R and T belong to S, and there exists a finite sequence Q1, Q2, …, Qk satisfying the following:

  1. Qi belongs to S (1 ≤ ik);
  2. Q1 = R, Qk = T;
  3. Qi ~ Qi+1 (1 ≤ ik−1) — i.e. Qi and Qi+1 are neighbours; and
  4. QiQj for any 1 ≤ i < jk

then we shall say that R and T are connected within set S, where the sequence Q1, Q2, …, Qk shall be called a pathway connecting points R and T.

Definition 4: For a set of integer points V, if for any two of V's integer points there exists exactly one pathway connecting them, then V shall be known as a singular set of integer points.

Definition 5: For any integer point on the plane, we can assign it an integer score. Thus, we shall call the sum of the scores of all the points in a set of integer points its total score.

Given a singular set of integer points V, we would like to find the optimally connected subset B, where:

  1. B is a subset of V;
  2. any two integer points in B is connected within B; and
  3. out of the set of integer points satisfying 1. and 2., B is the set where the total score is highest.

Input Format

Line 1 contains an integer N (2 ≤ N ≤ 1000), the number of points in V. Within the following N lines, the i-th line (1 ≤ iN) contains three space-separated integers Xi, Yi, and Ci (−106Xi, Yi ≤ 106; −100 ≤ Ci ≤ 100), representing the coordinates of the i-th point along with its score.

Output Format

The output should consist of one integer, the total score of the optimally connected subset.

Sample Input

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

Sample Output


All Submissions
Best Solutions

Point Value: 15 (partial)
Time Limit: 1.00s
Memory Limit: 16M
Added: May 04, 2014

Languages Allowed:

Comments (Search)

It's quiet in here...