Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
150668 jiayou 巡逻线路 C++ 解答错误 0 0 MS 208 KB 1438 2024-06-06 15:09:41

Tests(0/5):


#include <stdio.h> #include <math.h> #include <float.h> #define MAXN 1000 typedef struct { int x, y; } Point; Point points[MAXN]; double forward[MAXN]; double backward[MAXN]; // 计算两点之间的欧几里得距离 double dist(Point p1, Point p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } int main() { int n, b1, b2; scanf("%d %d %d", &n, &b1, &b2); for (int i = 0; i < n; i++) { scanf("%d %d", &points[i].x, &points[i].y); } // 初始化forward和backward数组 for (int i = 0; i < n; i++) { forward[i] = DBL_MAX; backward[i] = DBL_MAX; } forward[0] = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { double distance = dist(points[j], points[i]); if (forward[j] + distance < forward[i]) { forward[i] = forward[j] + distance; } } } backward[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { for (int j = n - 1; j > i; j--) { double distance = dist(points[j], points[i]); if (backward[j] + distance < backward[i]) { backward[i] = backward[j] + distance; } } } // 结果为forward[b1] + backward[b2] double result = forward[b1] + backward[b2]; printf("%.2f\n", result); return 0; }


测评信息: