第36651题 组合题
完善程序(第二题):最短哈密顿回路(状态压缩 DP)

(最短哈密顿回路)给定 n 个城市,任意两个城市 i 与 j 之间的距离为 w[i][j](w[i][i] = 0,距离不一定对称)。求从城市 0 出发,经过每个城市恰好一次并最终回到城市 0 的最短回路长度。保证 2 ≤ n ≤ 17,所有距离为不超过 10^6 的非负整数。以下代码用状态压缩动态规划求解。试补全程序。

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 18;
const int INF = 0x3f3f3f3f;

int n;
int w[MAXN][MAXN];
int dp[1 << MAXN][MAXN];

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            scanf("%d", &w[i][j]);
    ___①___;
    dp[1][0] = 0;
    for (int mask = 1; mask < (1 << n); ++mask)
        for (int i = 0; i < n; ++i) {
            if (___②___) continue;
            for (int j = 0; j < n; ++j) {
                if (___③___) continue;
                dp[mask | (1 << j)][j] = ___④___;
            }
        }
    int ans = INF;
    for (int i = 1; i < n; ++i)
        ___⑤___;
    printf("%d\n", ans);
    return 0;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
知识点:
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析