Module: Patterns in Dynamic Programming - 2


Problem

4 /5


Dwarf

Theory Click to read/hide

Disclaimer: The method described below is not universal, but it can often solve a problem or help you come to the right solution.

If you need to check the existence of a permutation in a problem, or find the number of permutations that match, or something similar, then you should consider using dynamic subset programming.

The main parameter will be a bitmask, which will show which elements have already been included in the permutation, and which are not. Also often required is a second parameter that indicates which element was included last.

Often permutations can be seen in the context of paths in graphs. Accordingly, let us consider a classical example - the Hamiltonian path problem.
A Hamiltonian path is a simple path that passes through each vertex of the graph exactly once. It can be represented simply as a permutation of length n, where n is the number of vertices. The order within this permutation will indicate the order in which the vertices in this path are traversed.

In order to check the presence of a Hamiltonian path in the graph, let's start the dynamics dp[mask][last] - is there a simple path that bypassed the vertices marked with ones in the mask bitmask and ended up at the last vertex.
The initial values ​​will be dp[2i][i] = true, the rest false, which means that there is always an empty path starting at any of the vertices.
Having the value true in dp[mask][last] we will make transitions forward, in the sense of "extending the path". That is, we will look for the numbers of vertices that are marked with zero in the mask (we have not yet visited them on the way) and at the same time such that there is an edge from last to this vertex (the path must be extended by an existing edge). That is, we do dp[mask | 2k][k] = true if dp[mask][last] = true AND mask & 2k = 0 (vertex k has not yet been visited) And there is an edge last -> k.
Ultimately, a Hamiltonian path exists if there is a true value in dp for the parameters of the all-ones bitmask and any last vertex.

Problem

Once the dwarf Quark came across a treasure map. There are N points marked on the map where the treasure can be found. All points are numbered from 1 to N. For each pair of points, Quark knows the length of the road connecting them. Quark starts his search from point number 1. Before starting his long journey, the cunning dwarf crosses out points where, in his opinion, there can be no treasure. It is guaranteed that point number 1 is never crossed out. After that, Quark chooses some route that passes through all the remaining points on the map. The route does not pass through the same point more than once. A quark can only walk on roads that connect uncrossed points.

Quark wants to choose a route of minimum length. It is necessary to find such a route for Quark.

Input
The first line contains one integer N (1 ≤ N ≤ 15) — the number of points marked on the map. The next N lines contain the distances between the points. The (i+1)-th line contains N integers di1,di2, diN — lengths of roads from the i-th point to all the others. It is guaranteed that dij=dji, dii=0 and 0 <dij <100 . The (N+2)th line contains one integer Q (1 < Q ≤ 1000) — the number of options for deleting points for this map. The following Q lines contain a description of the options for deletion. The description starts with the number C (0 ≤ C < N) — the number of points where, according to Quark, the treasure cannot be. The next C numbers give the numbers of these points.

Imprint
Print Q lines. In each line print one integer — the length of the minimum route with the corresponding option of deleting points.
Examples
# Input Output
1 3
0  45 10
45 0  30
10 30 0
2
0
1 3
40
45
2 5
0  14 20 17 14
14 0  15 19 18
20 15 0  15 16
17 19 15 0  14
14 18 16 14 0
2
3 5 4 3
0
14
58