Problem:
Problem F: Tighten Up!

We have a flat panel with two holes. Pins are nailed on its
surface. From the back of the panel, a string comes out through one
of the holes to the surface. The string is then laid on the surface
in a form of a polygonal chain, and goes out to the panel's back through
the other hole. Initially, the string does not touch any pins.

Figures F-1, F-2, and F-3 show three example layouts of holes, pins and
strings. In each layout, white squares and circles denote holes and
pins, respectively. A polygonal chain of solid segments denotes the
string.

Figure F-1: An example layout of holes, pins and a string

Figure F-2: An example layout of holes, pins and a string

Figure F-3: An example layout of holes, pins and a string

When we tie a pair of equal weight stones to the both ends of the
string, the stones slowly straighten the string until there is no
loose part. The string eventually forms a different polygonal chain as it is
obstructed by some of the pins. (There are also cases when the
string is obstructed by no pins, though.)

The string does not hook itself while being straightened. A fully
tightened string thus draws a polygonal chain on the surface of the
panel, whose vertices are the positions of some pins with the end
vertices at the two holes. The layouts in Figures F-1, F-2, and F-3 result in the
respective polygonal chains in Figures F-4, F-5, and F-6. Write a program that
calculates the length of the tightened polygonal chain.

Figure F-4: Tightened polygonal chains from the example in Figure F-1.

Figure F-5: Tightened polygonal chains from the example in Figure F-2.

Figure F-6: Tightened polygonal chains from the example in Figure F-3.

Note that the strings, pins and holes are thin enough so that you
can ignore their diameters.

Input

The input consists of multiple datasets, followed by a line containing
two zeros separated by a space. Each dataset gives the initial shape of the string (i.e., the positions of holes and vertices) and the positions of
pins in the
following format.

m n

x1 y1

...

xl yl

The first line has two integers m and n (2 ≤ m

≤ 100, 0 ≤ n ≤ 100), representing the number of
vertices including two holes that give the initial string shape (m) and the number
of pins (n). Each of the following l = m
+ n lines has two integers xi

and yi (0 ≤ xi ≤ 1000, 0 ≤ yi ≤ 1000), representing a position Pi =
(xi ,yi ) on the surface of the
panel.

Positions P1, ..., Pm give the initial shape of
the string; i.e., the two holes are at P1
and Pm , and the string's shape is a polygonal
chain whose vertices are Pi (i =
1, ..., m), in this order.

Positions Pm+1, ..., Pm+n are the positions of
the pins.

Note that no two points are at the same position. No three points are
exactly on
a straight line.

Output

For each dataset, the length of the part of the tightened string that
remains on the surface of the panel should be output in a line.
No extra characters
should appear in the output.

No lengths in the output should have an error greater than 0.001.

Sample Input

6 16
5 4
11 988
474 975
459 16
985 12
984 982
242 227
140 266
45 410
92 570
237 644
370 567
406 424
336 290
756 220
634 251
511 404
575 554
726 643
868 571
907 403
845 283
10 4
261 196
943 289
859 925
56 822
112 383
514 0
1000 457
514 1000
0 485
233 224
710 242
850 654
485 915
140 663
26 5
0 953
180 0
299 501
37 301
325 124
162 507
84 140
913 409
635 157
645 555
894 229
598 223
783 514
765 137
599 445
695 126
859 462
599 312
838 167
708 563
565 258
945 283
251 454
125 111
28 469
1000 1000
185 319
717 296
9 315
372 249
203 528
15 15
200 247
859 597
340 134
967 247
421 623
1000 427
751 1000
102 737
448 0
978 510
556 907
0 582
627 201
697 963
616 608
345 819
810 809
437 706
702 695
448 474
605 474
329 355
691 350
816 231
313 216
864 360
772 278
756 747
529 639
513 525
0 0

Output for the Sample Input

2257.0518296609
3609.92159564177
2195.83727086364
3619.77160684813

Solution in C++ (judge verdict: Accepted)

#include<cstdio>
#include<complex>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

typedef double Real;
typedef complex<Real> Point;

bool cmp(const Point &p1,const Point &p2){
	return p1.real()<p2.real();
}

Real sx[110],sy[110];
Real nx[110],ny[110];
int N,M;

Point ns[110];

vector<int> vec;

void init(){
	vec.clear();
}

void rot(){
	for(int i=1;i<M;i++){
		sx[i]-=sx[0];
		sy[i]-=sy[0];
	}
	for(int i=0;i<N;i++){
		nx[i]-=sx[0];
		ny[i]-=sy[0];
	}
	sx[0]=0,sy[0]=0;
	Point coe=Point(cos(1),sin(1));
//	Point coe=1;
	for(int i=1;i<M;i++){
		Point tmp=Point(sx[i],sy[i]);
		tmp*=coe;
		sx[i]=tmp.real();
		sy[i]=tmp.imag();
	}
	for(int i=0;i<N;i++){
		Point tmp=Point(nx[i],ny[i]);
		tmp*=coe;
		nx[i]=tmp.real();
		ny[i]=tmp.imag();
	}
	for(int i=0;i<N;i++){
		ns[i]=Point(nx[i],ny[i]);
	}
	sort(ns,ns+N,cmp);
	for(int i=0;i<N;i++){
		nx[i]=ns[i].real();
		ny[i]=ns[i].imag();
	}
}

void calc(Real sx,Real sy,Real tx,Real ty){
//	printf("%f %f %f %f\n",sx,sy,tx,ty);
	if(sx<tx){
		for(int i=0;i<N;i++){
			if(sx<nx[i]&&nx[i]<tx){
				Real r=(nx[i]-sx)/(tx-sx);
				Real dy=(ty-sy)*r;
				Real y=sy+dy;
				int id;
				if(y<ny[i]) id=i*2;
				else id=i*2+1;
//				printf("id=%d\n",id);
				if(vec.size()==0||vec.back()!=id) vec.push_back(id);
				else vec.pop_back();
			}
		}
	}else{
		for(int i=N-1;i>=0;i--){
			if(tx<nx[i]&&nx[i]<sx){
				Real r=(nx[i]-sx)/(tx-sx);
				Real dy=(ty-sy)*r;
				Real y=sy+dy;
				int id;
				if(y<ny[i]) id=i*2;
				else id=i*2+1;
//				printf("id=%d\n",id);
				if(vec.size()==0||vec.back()!=id) vec.push_back(id);
				else vec.pop_back();
			}
		}
	}
}

Real dp[10100];

Point ps[10100];
bool dirs[10100];

Real solve(){
	init();
	rot();
	for(int i=0;i<M-1;i++){
		calc(sx[i],sy[i],sx[i+1],sy[i+1]);
	}
	if(vec.size()==0){
		Real dx=sx[0]-sx[M-1],dy=sy[0]-sy[M-1];
		return sqrt(dx*dx+dy*dy);
	}
	ps[0]=Point(sx[0],sy[0]);
	for(int i=0;i<vec.size();i++){
		ps[i+1]=ns[vec[i]/2];
		dirs[i+1]=vec[i]%2==1;
	}
	ps[vec.size()+1]=Point(sx[M-1],sy[M-1]);
	dp[0]=0;
//	for(int i=0;i<vec.size()+2;i++){
//		printf("%f %f %c\n",ps[i].real(),ps[i].imag(),dirs[i]?'u':'d');
//	}
	for(int i=1;i<vec.size()+2;i++){
		dp[i]=dp[i-1]+abs(ps[i]-ps[i-1]);
		for(int j=i-2;j>=0;j--){
			if(dp[i]<dp[j]+abs(ps[i]-ps[j])) continue;
			if(abs(ps[j]-ps[j+1])<(1e-8)) break;
			bool ok=true;
			for(int k=j+1;k<i;k++){
				Real x1=ps[j].real(),y1_=ps[j].imag();
				Real x2=ps[i].real(),y2_=ps[i].imag();
				Real x=ps[k].real();
				Real r=(x-x1)/(x2-x1);
				Real y=y1_+(y2_-y1_)*r;
				bool flg=ps[k].imag()<y;
				if(dirs[k]!=flg){
					ok=false;
					break;
				}
			}
			if(ok){
				dp[i]=dp[j]+abs(ps[i]-ps[j]);
			}
		}
	}
//	for(int i=0;i<vec.size()+2;i++){
//		printf("%f ",dp[i]);
//	}
//	printf("\n");
	return dp[vec.size()+1];
}

void input(){
	scanf("%d%d",&M,&N);
	if(M==0&&N==0) exit(0);
	for(int i=0;i<M;i++){
		scanf("%lf%lf",sx+i,sy+i);
	}
	for(int i=0;i<N;i++){
		scanf("%lf%lf",nx+i,ny+i);
	}
}

int main(){
	while(true){
		input();
		Real ans=solve();
		printf("%f\n",ans);
	}
	return 0;
}