Golf Courses
InterviewTime limit1sMemory limit128 MB
Pick course sites and assign every client to a built course to minimize building plus connection costs within capacities.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy
- Solved
- No attempts yet
Problem
We want to build golf courses near the New Light apartment complex. Several candidate sites have been surveyed, and for each candidate we know its capacity (the number of clients it can serve) and its construction cost.
Many residents of New Light want to play golf. We must choose some of the candidate sites, build golf courses on them, and assign every client to one of the built courses so that all clients are served, while making the total cost as small as possible.
The total cost has two parts:
- the total connection cost of the clients to the courses they use, plus
- the total construction cost of the courses that are built.
Consider the example below.

The links between the candidate sites and New Light form a star graph: New Light is the center vertex, and every other vertex is a candidate site. The number next to New Light is the number of clients who want to play golf. The two numbers next to a candidate site are its construction cost and its capacity. The number on an edge is the distance between New Light and that candidate site; a client who uses that course pays this distance as a connection cost.
For this example, the optimal solution is shown below. Two courses are built, with construction costs and . Two clients use the course whose connection cost (distance) is , and the remaining three clients use the course whose connection cost is . The total construction cost is , and the total connection cost is , so the total cost is .

In the examples, the first test case corresponds to the figures above.
Input
The input is read from standard input and consists of test cases. The first line contains the number of test cases ().
Each test case has the following form. The first line contains a positive integer (), the number of candidate sites. The second line contains a positive integer (), the number of clients. Each of the next lines describes one candidate site with three positive integers separated by single spaces: the distance from New Light to the site, the construction cost of the course, and the capacity of the course. Every integer is between and , inclusive.
The total capacity of the candidate sites is at least , so all clients can always be served.
Output
Write to standard output. For each test case, print on its own line the minimum possible total cost, that is, the smallest achievable sum of the total connection cost and the total construction cost.