![:cry:](./images/smilies/icon_cry.gif)
My algo is simple:
1. For each segment AB do
1.a Check if points A or B can be choosen (It's possible if cos(A) <= 0 or cos(B) <=0))
1.b Else try to find such point X on AB that XM is minimal
1.c Check if the point X that we found is better than points that we found before
2 Output X
Anyway, it gets WA (But I think that my algo is correct)
My program
[pascal]Program p10263; {$n+}
Const Eps = 1e-7;
Eps2 = 1e-5;
Type Point = Record x, y : Extended End;
Var M, S : Point;
N, i : Integer;
R, Rp : Point;
Function Dist(A, B : Point) : Extended;
begin
Dist := Sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y));
end;
Function GetCosA(A, B, C : Point) : Extended;
Var la, lb, lc : Extended;
begin
la := Dist(B, C);
lb := Dist(A, C);
lc := Dist(A, B);
GetCosA := (lb * lb + lc * lc - la * la) / 2 / lb / lc;
end;
Function FindMinPoint(A, B, M : Point; Var X : Point) : Extended;
Var cosa, cosb, len : Extended;
TX : Point;
begin
cosa := GetCosA(A, B, M);
cosb := GetCosA(B, A, M);
if (cosa < Eps) Or (cosb < Eps) then begin
if Dist(A, M) < Dist(B, M) then TX := A else TX := B;
if Dist(M, TX) < Dist(M, X) then X := TX;
Exit;
end;
len := Dist(A, M) * cosa;
TX.x := A.x + len / Dist(A, B) * (B.x - A.x);
TX.y := A.y + len / Dist(A, B) * (B.y - A.y);
if Dist(M, TX) < Dist(M, X) then X := TX;
end;
begin
While Not Eof Do begin
Readln(M.x);
Readln(M.y);
Readln(N);
Readln(Rp.x);
Readln(Rp.y);
S := Rp;
for i := 1 to N do begin
Readln(R.x);
Readln(R.y);
FindMinPoint(R, Rp, M, S);
Rp := R;
end;
if Abs(S.x) > Eps2 then Writeln(S.x:0:4) else Writeln('0.0000');
if Abs(S.y) > Eps2 then Writeln(S.y:0:4) else Writeln('0.0000');
end;
end.[/pascal]