hdu2671

来源:互联网 发布:安装ubuntu u盘 编辑:程序博客网 时间:2024/06/06 17:50

Can't be easier

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 796    Accepted Submission(s): 364


Problem Description
I'm sure this problem will fit you as long as you didn't sleep in your High School Math classes.
Yes,it just need a little math knowledge and I know girls are always smarter than we expeted.
So don't hesitate any more,come and AC it!
Tell you three point A,B,C in a 2-D plain,then a line L with a slope K pass through C,you are going to find
a point P on L,that makes |AP| + |PB| minimal.
 

Input
The first line contain a t.
Then t cases followed,each case has two parts,the first part is a real number K,indicating the slope,and the second
part are three pairs of integers Ax,Ay,Bx,By,Cx,Cy(0 <=|Ax|,|Ay|,|Bx|,|By|,|Cx|,|Cy| <= 10000 ).
 

Output
Just out put the minimal |AP| + |PB|(accurate to two places of decimals ).
 

Sample Input
12.558467 6334 6500 9169 5724 1478
 

Sample Output

3450.55

AC代码:

#include<iostream>#include<cmath>#include<cstdio>using namespace std;int main(){        int c;        double k,x1,y1,x2,y2,x3,y3,x,y,dis,t,a,b;        cin>>c;        while(c--)        {                cin>>k>>x1>>y1>>x2>>y2>>x3>>y3;                if(x3!=1.0)                        t=k-k*x3+y3; //用直线上的(1,t)和(x3,y3)表示这条直线L                else                        t=2*k-k*x3+y3;  //用直线上的(2,t)和(x3,y3)表示这条直线L                a=(1-x3)*(y2-y3)-(t-y3)*(x2-x3);                b=(1-x3)*(y1-y3)-(t-y3)*(x1-x3);                if(a*b>0) //a,b为AC,BC两直线和直线L的叉积,异号则在直线异侧                {                        y=(2*y3+(k*k-1)*y2+2*k*(x2-x3))/(k*k+1); //联立向量(CA+CB)所在直线的斜率为k和向量AB所在直线的斜率为-1/k两个方程解得B`                        x=x2-k*(y-y2);                        dis=sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));                }                else                        dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));                printf("%.2lf\n",dis);        }        return 0;}