def compute_route_cost(dist, route):
    """Compute the cost of a route."""
    N = len(route)
    assert N == len(dist) + 1
    assert route[0] == route[-1]
    cost = 0
    for i in range(1, len(route)):
        u = route[i - 1]
        v = route[i]
        c = dist[u][v]
        assert c != 0
        cost += c
    return cost