給四邊形四個頂點(順序隨機),求其費馬點(使到四頂點距離和最小的點)的最小距離和。輸出保留 4 位小數。
struct Point { double x, y; };
double cross(Point a, Point b, Point c) { // (b-a)×(c-a)
return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}
double dist(Point a, Point b) { return hypot(a.x-b.x, a.y-b.y); }
int graham(Point p[], int n, Point ch[]) {
sort(p, p+n, [](Point a, Point b){ return a.y=2 && cross(ch[top-2], ch[top-1], p[i]) <= 0) --top;
ch[top++] = p[i];
}
int lower = top;
for (int i=n-2;i>=0;--i) {
while (top>lower && cross(ch[top-2], ch[top-1], p[i]) <= 0) --top;
ch[top++] = p[i];
}
return top-1; // 凸包點數
}
POJ 3990 利用四邊形費馬點的幾何性質,配合凸包判斷凸/凹,極簡解決。關鍵洞見:凸四邊形費馬點為對角線交點,凹四邊形為凹點。無需模擬退火等啟發式算法。