Travelling Salesman in Qt
I'd have serious doubts about its being the fastest possible code, but just for fun let's consider the simplest possible code to generate correct answers.

The standard library has next_permutation, which can generate all the possible permutations of a collection. So, let's consider how we do this. We start with a 2D array of distances between cities. We then have a collection to represent a route through the cities. Since each city must appear once in the collection, we can start with it just containing numbers from 0 to N in order. We then permute those, find the distance for each, keep the shortest, and we're (eventually) done.

#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt;
#include &lt;limits&gt;

static const int max = 13;

void setup(int distances[max][max]) {
    for (int i = 0; i &lt; max; i++) {
        distances[i][i] = 0;
        for (int j = i + 1; j &lt; max; j++) {
            int d = rand() % 50;

            distances[i][j] = d;
            distances[j][i] = d;
        }
    }
}

int main() {
    int distances[max][max];

    setup(distances);

    std::vector&lt;int&gt; route(max);

    std::iota(route.begin(), route.end(), 0);

    int best_distance = std::numeric_limits&lt;int&gt;::max();
    do {
        int distance = 0;
        for (int i = 1; i &lt; route.size(); i++) {
            distance += distances[route[i - 1]][route[i]];
            if (distance &gt; best_distance)
                break;
        }
        distance += distances[route[max - 1]][0];

        if (distance &lt; best_distance) best_distance = distance;
    } while (std::next_permutation(route.begin() + 1, route.end()));

    std::cout &lt;&lt; "best distance: " &lt;&lt; best_distance &lt;&lt; "\n";
}


Now some might wonder why I'd question this being the fastest way to do the job. Generic algorithms are, after all, intended to provide nearly the speed of custom-written code--their runtime overhead is usually quite minimal.

While that's true, in this case an important optimization is missed. If we view the routes as a tree or a graph, we can stop traversing a branch as soon as we reach a node where the current distance is greater than the best distance so far. This can avoid visiting a large number of paths based on a single test.

Using next_permutation, however we don't get that optimization. Since we don't know the relationships between paths, information we obtain from traversing one path can't be used to eliminate other possible paths.

While probably pretty fast for small sets of data, this is likely to be substantially slower than many others when N gets large at all.

Note: this does not depend upon the triangle inequality (and the test data it generates almost certainly doesn't follow the triangle inequality either).