Problem H
HARDLY HARD
You have been given the task of cutting out a quadrilateral slice of cake out of a larger, rectangular cake. You must find the slice with the smallest perimeter that satisfies the following constraints. If the cake is of size 10000-by-10000 units and is represented using the first quadrant of the Cartesian plane, then your slice is quadrilateral ABCD (see figure). Points A and B are fixed and will be given to you. Also, A,B will lie on a negatively sloping line. Furthermore, points C and D must lie on the positive y-axis and positive x-axis respectively, but it is up to you to determine where these two points should be. A,B,C,D will be distinct points.
Output the minimum perimeter of your slice of cake.
Input
On the first line you will be given n (1 ≤ n ≤ 100), the number of test cases. The following n lines each contain ax ay bx by (0 < ax, ay, bx, by ≤ 10000.0), the coordinates of points A and B respectively.
Output
For each test case, output the perimeter accurate to 3 decimal places on its own line.
题意:由一个矩形蛋糕切出一个四边形,要求周长最小;其中A,B两点坐标已给出,且C,D点要求在x,y轴正半轴。
分析:以x,y轴作对称点连接。证明很好证,两点之间线段最短嘛。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define error 1e-8 6 using namespace std; 7 const int maxn = 10010; 8 9 10 double Get_len(double x1, double y1, double x2, double y2) 11 { 12 return sqrt((y2-y1)*(y2-y1) + (x2-x1)*(x2-x1)); 13 } 14 int main() 15 { 16 17 int T; 18 scanf("%d", &T); 19 while(T--) 20 { 21 double ax, ay, bx, by; 22 scanf("%lf%lf%lf%lf", &ax, &ay, &bx, &by); 23 if(by < ay) {swap(ay, by); swap(ax, bx);} 24 double cx=ax, cy=-ay, dx=-bx, dy=by; 25 printf("%.3lf\n", Get_len(cx, cy, dx, dy)+Get_len(ax, ay, bx, by)); 26 } 27 return 0; 28 }