# SCIENCEF - Editorial

Practice

Contest

Author: Utkarsh Saxena

Tester: Alexey Zayakin

Editorialist: Utkarsh Saxena

### PROBLEM

You are given a graph of V nodes and E weighted edges, a source node S, destination node F and a special set of N nodes. You are given a value T_i for each special node. For each subset of special nodes find the minimum cost of path from S to T covering all the nodes in the subset.

Cost(path) = Length(path) + \big(\prod_{\text{special node in path}}{T_i}\big)\space mod\space 10^9+7

Constraints: n \le 16, V \le 1000, E \le 3000.

### PREREQUISITES

Traveling salesman problem

### EXPLANATION

Notice that the cost of path is affected by the set of special nodes that lie on it.

Given a set of special nodes to cover \big\{S_0, S_1,.., S_k\big\}. A path that covers these nodes will start from S, cover all the given special nodes, cover some extra special node and then end at F. Let the extra special nodes covered = \big\{S_{e0}, S_{e1},.., S_{eg}\big\}.

Cost(path) = Length(path) + \big(T_0 * T_1 *.. * T_k * T_{e0} * T_{e1} *..* T_{eg} \big)\space mod\space 10^9+7.

So the cost is also dependent on the extra special nodes that you cover apart from the required nodes.

### Simpler problem

Let’s assume that we are not allowed to visit any extra special node apart from the given subset of special nodes. For this it is sufficient to minimize the length of the path required in order to minimize the cost of path since the \prod part of cost is constant for this subset.

To solve this problem find SP_{ij} = Shortest path from special node i to special node j that does not include any other special node. For each subset of special node: using this matrix you can calculate the shortest path covering exactly those special nodes.

Denote dp[mask][i] = Length of shortest path that starts from S, ends at special node i and covers exactly those special nodes denoted by the ON bits of mask.

In order to end at this state, previous state can be

• We have visited all nodes in mask and are currently at j^{th} node.
• We have visited all nodes in mask except i and are currently at j^{th} node.

Notice that this recurrence contains a cyclic dependency. We can solve this recurrence using Dijsktra shortest path algorithm on the graph made by the transitions of above recurrence.

Denote dp2[mask] = min(dp[mask][i] + sp[i][F]) + prod[mask]. This will denote the minimum cost of path that begins at S, ends at F and covers only the special nodes mentioned in mask.
Here prod[mask] = product of T_i of all i present in mask.