-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadMe
More file actions
7 lines (7 loc) · 738 Bytes
/
ReadMe
File metadata and controls
7 lines (7 loc) · 738 Bytes
1
2
3
4
5
6
7
The following solution is based on such an idea:
let us have a function min(mask, c), where mask is the set of cities we've visited and c is the city we visited the last.
The set is a binary mask(for example 1001), where zero means that we haven't visited the city yet and one means that we have.
For all possible states of mask and c we calculate the shortest distance using dynamic programming:
If we want to go from c to some city c2 we haven't visited yet, from (mask, c) we go to (mask | c2,, c2), and the answer is
(mask | c2, c2) = (mask, c1) + dist(c1, c2)
The overall complexicy of the algorithm is (2^n * n^2), since we make 2^n iterations to get all posible masks and also n^2 iterations to determine c and c2 for such masks.