Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 E
Four cities have symmetric travel costs D = [[0, 27, 24, 21], [27, 0, 30, 17], [24, 30, 0, 1], [21, 17, 1, 0]]. Starting at city 0, you must visit every city exactly once and return to city 0 at the minimum total cost. What is that minimum cost?
- A. 79
- B. 69
- C. 72
- D. 74
Correct answer: B. 69
Bitmask DP: dp[mask][last] is the cheapest path from city 0 that visits exactly the cities in mask and ends at last. Greedy nearest-neighbour gives a good but not always optimal tour, and the answer adds the return leg to city 0.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A directed graph on vertices 0 to 3 has adjacency matrix [[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 0]] (adj[i][j] = 1 means an…
- For the list [3, 5, 6, 9, 10], how many subsets (including the empty subset) have a bitwise XOR of exactly 0?
- How many subsets of the set {1, 2, ..., 10} (the empty set included) contain no two consecutive integers?
- In how many ways can 6 distinct people be divided into 3 unordered pairs (every person is in exactly one pair, and the pairs are not…
- Items are given as (weight1, weight2, value): [(2, 3, 4), (3, 4, 5), (4, 6, 2), (1, 1, 1), (5, 7, 8)]. Each item can be used once. You…
- How many ways can the six numbers [1, 2, 3, 4, 5, 6] be assigned to three labelled groups (group A, group B, group C), each number going…