Power Generation

Time limit3sMemory limit128 MB

Summary
Build the tree formed by attaching each new plant to the nearest older one, then split it into the most connected subtrees each having total capacity at least C.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Greedy, Geometry
Solved
No attempts yet

Problem

Demand for electricity has grown rapidly in the country over recent years, and it is projected to grow even faster over the next twenty years. To cope with this increase, the government plans to privatize the country's electricity power-generation sector, ending the monopoly of the state-owned company ICPC (Independent Circuit Power Corporation).

ICPC owns a set of power plants (hydroelectric and nuclear). The plants are connected by power lines that cross the country. Each power line connects two distinct plants and is a straight segment. A power path is a sequence of power lines l1,l2,…,lml_1, l_2, \ldots, l_m in which each line lil_i directly connects plants pi−1p_{i-1} and pip_i, and any two consecutive lines lil_i and li+1l_{i+1} share a common plant pip_i.

The plants were built over several years, one at a time, because of budget limits. Also because of budget limits, whenever a new plant was built, exactly one new power line was constructed to attach it to the existing system. That new line always connected the new plant to the nearest plant already in the system, measured by Euclidean distance. If more than one such plant was at the minimum distance, the oldest (earliest built) one was chosen.

The goal of the privatization is to split the ICPC system into smaller companies. Each company owns a set of plants, and every plant is owned by exactly one company. After privatization ICPC ceases to exist and only the new companies own plants. The split must satisfy:

  • The total capacity of every new company must be at least CC, a value in MW (megawatts) set by the government. The total capacity of a set of plants is the sum of the plants' capacities.
  • For any two plants owned by a company, every power path between them must pass only through plants owned by that same company.

Determine the largest number of new companies that can be created in the privatization process.

Input

The input contains several test cases. The first line of a test case has two integers NN and CC: the total number of power plants owned by ICPC (1≤N≤100001 \le N \le 10000) and the minimum total capacity, in MW, that every new company must have (1≤C≤100001 \le C \le 10000). Plants are identified by integers from 11 to NN; plant 11 was built first, plant 22 second, and so on. Each of the next NN lines describes one plant: the first of these lines describes plant 11, the second describes plant 22, and so on. Each description has three integers XX, YY, and PP, where (X,Y)(X, Y) is the plant's location (0≤X≤10000 \le X \le 1000 and 0≤Y≤10000 \le Y \le 1000) and PP is its capacity (1≤P≤10001 \le P \le 1000). No two plants share the same location. The end of input is indicated by N=C=0N = C = 0.

Output

For each test case, print a single line containing one integer: the largest number of new companies that can be created in the privatization process.

Examples1

  1. Example 1

    Input
    2 22
    0 0 20
    10 20 30
    4 430
    10 20 100
    20 10 400
    50 10 50
    25 25 500
    3 100
    10 10 33
    0 10 33
    10 0 33
    0 0
    
    Expected output
    1
    2
    0