Network Expansion

Time limit1sMemory limit128 MB

Summary
Given a connected rail network, up to 10 priced expansion routes, and a passenger demand matrix, pick an affordable subset that maximizes the drop in total travel time.
Level

Medium7 of 10

Topics
Graph, Brute force, Shortest path, Implementation
Solved
No attempts yet

Problem

A city is expanding its public rail network. Several expansion routes are being considered, but the budget is limited, so only some of them can be built. Your task is to choose the affordable subset of proposed routes that reduces the total passenger travel time as much as possible.

You are given the current rail system and up to 10 proposed expansion routes. Choose a subset of the proposed routes whose total price does not exceed the budget so that the total travel time of all passengers is reduced by the largest possible amount.

Travel-time model: riding from one station to an adjacent station on any route takes exactly one minute, and transferring between routes at a shared station is instantaneous. Every route can be traveled in both directions. Even before any expansion the current system is already connected, so every station is reachable from every other station. The travel time between two stations is therefore the minimum number of station-to-station hops between them.

Input

The first line contains the number of data sets KK. Each data set then follows in this form:

  • The first line contains four integers nn, mm, pp, and BB: the number of stations nn (2≤n≤502 \le n \le 50), the number of current routes mm (1≤m≤501 \le m \le 50), the number of proposed expansion routes pp (1≤p≤101 \le p \le 10), and the total budget BB.
  • The next mm lines each describe one current route. A route line lists ni≥2n_i \ge 2 integers: the stations the route visits, in order.
  • The next pp lines each describe one proposed expansion route jj. Such a line starts with one integer pjp_j, the price of the route, followed by nj′≥2n'_j \ge 2 integers giving the stations the route visits, in order.
  • Finally, nn lines follow, each with nn integers. The jj-th integer on the ii-th of these lines is the number of passengers who want to travel from station ii to station jj.

Stations are numbered from 11 to nn.

Output

For each data set, output a line Data Set x:, where xx is the data set number (starting at 11). On the next line, output a single integer: the maximum total reduction in the sum of all passengers' travel times that can be achieved by building a subset of the proposed routes whose combined price does not exceed the budget.

Examples5

  1. Example 1

    Input
    1
    6 1 3 5
    1 2 3 4 5 6
    3 1 3
    3 4 2
    4 2 6
    0 0 2 5 0 10
    10 0 6 5 20 2
    2 8 0 7 9 10
    3 2 5 0 5 1
    6 6 17 2 0 12
    0 1 4 12 7 0
    
    Expected output
    Data Set 1:
    85
    
  2. Example 2

    Input
    1
    3 1 2 0
    1 2 3
    5 1 3
    7 1 2
    0 5 10
    5 0 3
    2 8 0
    
    Expected output
    Data Set 1:
    0
    
  3. Example 3

    Input
    1
    4 1 1 2
    1 2 3 4
    2 1 4
    0 1 1 10
    1 0 1 1
    1 1 0 1
    20 1 1 0
    
    Expected output
    Data Set 1:
    60
    
  4. Example 4

    Input
    1
    4 1 2 2
    1 2 3 4
    5 1 4
    2 2 4
    0 1 1 5
    1 0 1 8
    1 1 0 1
    5 8 1 0
    
    Expected output
    Data Set 1:
    26
    
  5. Example 5

    Input
    2
    6 1 3 5
    1 2 3 4 5 6
    3 1 3
    3 4 2
    4 2 6
    0 0 2 5 0 10
    10 0 6 5 20 2
    2 8 0 7 9 10
    3 2 5 0 5 1
    6 6 17 2 0 12
    0 1 4 12 7 0
    3 1 1 0
    1 2 3
    5 1 3
    0 5 10
    5 0 3
    2 8 0
    
    Expected output
    Data Set 1:
    85
    Data Set 2:
    0