求助!厌氧代码。
查看原帖
求助!厌氧代码。
1788032
BlackHoles楼主2025/7/19 10:30

开 O2 WA on #10,Hack 全过不了。不开全过,求解答与避免类似情况的方法。

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
constexpr int N = 16;
constexpr int A = (1 << N) - 1;
const ld INF = 2e9;
int n;
struct node {
	ld x, y;
} a[N];
ld dp[A][N];
ld dis(int x, int y) {
	ld res = sqrt(pow(a[x].x - a[y].x, (ld)2) + pow(a[x].y - a[y].y, (ld)2));
	return res;
}
int main(void) {
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(2);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i].x >> a[i].y;
	a[0].x = a[0].y = 0;
	int AN = (1 << (n + 1)) - 1;
	for (int i = 0; i <= AN; ++i)
		for (int j = 0; j <= n; ++j)
			dp[i][j] = INF;
	dp[1][0] = 0;
	for (int S = 2; S <= AN; ++S)
		for (int j = 0; j <= n; ++j)
			if ((S >> j) & 1)
				for (int k = 0, T; k <= n; ++k)
					if (((T = S ^ (1 << j)) >> k) & 1)
						dp[S][j] = min(dp[S][j], dp[T][k] + dis(k, j));
	ld ans = INF;
	for (int j = 0; j <= n; ++j)
		ans = min(ans, dp[AN][j]);
	cout << ans;
	return 0;
}
2025/7/19 10:30
加载中...