Servicing Clients
Time limit1sMemory limit128 MB
Choose clients whose distance-times-demand costs fit the budget to maximize total priority.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Shortest path
- Solved
- No attempts yet
Problem
One facility and several clients sit at some of the vertices of a weighted graph . Every client comes with a nonnegative demand and a nonnegative priority that says how important that client is.
The servicing cost of client is , where is the shortest distance in from the facility vertex to the vertex of client . In the figure below the servicing cost of client is , where is the demand of client . The servicing cost of client in the same figure is infinite, because no path connects client to the facility.
Write a program that picks a subset of the client set whose total servicing cost stays within a nonnegative budget and whose total priority is as large as possible.

Input
Your program reads from standard input. The first line contains the number of test cases . ()
Each test case starts with a line containing the number of vertices of the graph. () The vertices are numbered from to , and the facility sits at vertex .
The next line contains the number of clients . () Each of the next lines describes one client with three integers: the vertex the client sits at, the demand, and the priority. The vertex number is between and , and the demand and the priority are each between and .
The next line contains the total budget . ()
The next line contains the number of edges . () Each of the next lines describes one edge with three integers: the two vertices the edge joins, then the cost of the edge. The vertex numbers are between and and the cost is between and . Edges have no direction.
Output
Your program writes to standard output. For each test case, print one line with the total priority of the clients selected for service.