題目連結

For every $dp[i] = k$, we define $i$ as the collection of people that’re not paired yet, and $k$ is the minimum cost to pair those people up, if possible.

Starting with (1 << n) - 1, we can pair 2 of them at a time, and recurse on (state ^ (1 << i) ^ (1 << j)).

Notice the base case is $dp[0] = 0$.

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const double oo = 1e9;

double dist[22][22];

void dfs(int s, double dp[], int n)
{
	if (dp[s] != oo)
		return;
	// 1 means not paired uet
	for (int i = 0; i < n; i++) {
		if ((s >> i) & 1) {
			for (int j = i + 1; j < n; j++) {
				if (i == j) // pair with self, cont.
					continue;
				if ((s >> j) & 1) {
					int nxt = s ^ (1 << i) ^ (1 << j);
					dfs(nxt, dp, n);
					dp[s] = min(dp[s], dp[nxt] + dist[i][j]);
				}
			}
		}
	}
}

double solve(int n)
{
	n *= 2;
	ii pos[n];
	for (int i = 0; i < n; i++) {
		char inp[111];
		scanf("%s %d %d", inp, &pos[i].first, &pos[i].second);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int dx = pos[i].first - pos[j].first;
			int dy = pos[i].second - pos[j].second;
			dist[i][j] = sqrt(dx * dx + dy * dy);
		}
	}

	// for (int i = 0; i < n; i++)
	// 	for (int j = 0; j < n; j++) {
	// 		printf("%7.2f%c", dist[i][j], j == n - 1 ? '\n' : ' ');
	// 	}

	double dp[1 << n];
	fill(dp, dp + (1 << n), oo);
	dp[0] = 0; // base case
	dfs((1 << n) - 1, dp, n);
	return dp[(1 << n) - 1];
}

int main()
{
	int n;
	for (int i = 1; scanf("%d", &n) == 1 && n; i++) {
		printf("Case %d: %.2f\n", i, solve(n));
	}

	return 0;
}