Files
python/TangDou/LuoGuBook/P1433.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

47 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 16; //对于全部的测试点,保证 1<=n<=15,
int n; //一共多少个奶酪
double res = INF; //记录最短路径长度,也就是最终的答案
double dis[N][N]; //dis[i][j]记录第i个点到第j的点的距离.这个是预处理的二维数组,防止重复计算,预处理是搜索优化的重要手段
//坐标
struct Point {
double x, y;
} a[N];
int per[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int main() {
//老鼠的原始位置
a[0].x = 0, a[0].y = 0;
//读入奶酪的坐标
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
//预处理
for (int i = 0; i < n; i++)
for (int j = i + 1; j <= n; j++) {
double x1 = a[i].x, y1 = a[i].y, x2 = a[j].x, y2 = a[j].y;
dis[j][i] = dis[i][j] = sqrt(abs((x1 - x2) * (x1 - x2)) + abs((y1 - y2) * (y1 - y2)));
}
//1~n的全排列,计算每一组组合的距离和,找出最小值,也就能过70%的测试点,11个数据是极限
double MIN = INF;
do {
//计算当前路线下的行走距离
double s = dis[0][per[0]];
for (int i = 0; i < n - 1; i++) s += dis[per[i]][per[i + 1]];
MIN = min(MIN, s);
} while (next_permutation(per, per + n));
//输出大吉
printf("%.2f", MIN);
return 0;
}