어려움 0명 해결 정답률 0%

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.

제약 조건

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

예시

예시 1
입력:
distances = [
    [0, 10, 15, 20],
    [10, 0, 35, 25],
    [15, 35, 0, 30],
    [20, 25, 30, 0]
]

예상 출력:
expected = 80
입력:
std::vector<std::vector<long long>> distances = {
    {0, 10, 15, 20},
    {10, 0, 35, 25},
    {15, 35, 0, 30},
    {20, 25, 30, 0}
};

예상 출력:
long long expected = 80;
입력:
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}
};

예상 출력:
long expected = 80;

설명

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

예시 2
입력:
distances = [
    [0]
]

예상 출력:
expected = 0
입력:
std::vector<std::vector<long long>> distances = {
    {0}
};

예상 출력:
long long expected = 0;
입력:
long[][] distances = new long[][] {
    new long[] {0}
};

예상 출력:
long expected = 0;

설명

With one city there is nowhere to go: 0.

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.