Retour aux défis
Difficile 0 résolus 0 % de réussite

Shortest Delivery Tour

Visit every city once and return home as cheaply as possible.

A courier starts at city 0, visits every other city exactly once and returns to city 0. distances[i][j] is the cost from i to j. With at most 15 cities, a bitmask dynamic program (Held–Karp) over visited sets is fast enough.

Implement shortest_tour(distances).

Input

distances: An n × n matrix of travel costs; distances[i][i] = 0.

Output

Return the cost of the cheapest round trip.

Contraintes

1 ≤ n ≤ 15; 0 ≤ cost ≤ 1,000,000

Exemples

Exemple 1
Entrée :
distances = [
    [0, 10, 15, 20],
    [10, 0, 35, 25],
    [15, 35, 0, 30],
    [20, 25, 30, 0]
]

Sortie attendue :
expected = 80
Entrée :
std::vector<std::vector<long long>> distances = {
    {0, 10, 15, 20},
    {10, 0, 35, 25},
    {15, 35, 0, 30},
    {20, 25, 30, 0}
};

Sortie attendue :
long long expected = 80;
Entrée :
long[][] distances = new long[][] {
    new long[] {0, 10, 15, 20},
    new long[] {10, 0, 35, 25},
    new long[] {15, 35, 0, 30},
    new long[] {20, 25, 30, 0}
};

Sortie attendue :
long expected = 80;

Explication

0→1→3→2→0 costs 10 + 25 + 30 + 15 = 80, the cheapest tour.

Exemple 2
Entrée :
distances = [
    [0]
]

Sortie attendue :
expected = 0
Entrée :
std::vector<std::vector<long long>> distances = {
    {0}
};

Sortie attendue :
long long expected = 0;
Entrée :
long[][] distances = new long[][] {
    new long[] {0}
};

Sortie attendue :
long expected = 0;

Explication

With one city there is nowhere to go: 0.

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.