Problem:
Repairing

In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network.
The network consists of water pipe segments, stop valves and source point.
A water pipe is represented by a segment on a 2D-plane and intersected pair of water pipe segments are connected at the intersection point.
A stop valve, which prevents from water flowing into the repairing point while repairing, is represented by a point on some water pipe segment.
In the network, just one source point exists and water is supplied to the network from this point.

Of course, while repairing, we have to stop water supply in some areas, but,
in order to reduce the risk of riots, the length of water pipes stopping water supply must be minimized.
What you have to do is to write a program to minimize the length of water pipes needed to stop water supply when the coordinates of end points of water pipe segments, stop valves, source point and repairing point are given.

Input

A data set has the following format:

N M

xs1 ys1 xd1 yd1

...

xsN ysN xdN ydN

xv1 yv1

...

xvM yvM

xb yb

xc yc

The first line of the input contains two integers, N (1 ≤ N ≤ 300) and M (0 ≤ M ≤ 1,000) that indicate the number of water pipe segments and stop valves.
The following N lines describe the end points of water pipe segments.
The i-th line contains four integers, xsi, ysi, xdi and ydi that indicate the pair of coordinates of end points of i-th water pipe segment.
The following M lines describe the points of stop valves.
The i-th line contains two integers, xvi and yvi that indicate the coordinate of end points of i-th stop valve.
The following line contains two integers, xb and yb that indicate the coordinate of the source point.
The last line contains two integers, xc and yc that indicate the coordinate of the repairing point.

You may assume that any absolute values of coordinate integers are less than 1,000 (inclusive.)
You may also assume each of the stop valves, the source point and the repairing point is always on one of water pipe segments and that that each pair among the stop valves, the source point and the repairing point are different.
And, there is not more than one intersection between each pair of water pipe segments.
Finally, the water pipe network is connected, that is, all the water pipes are received water supply initially.

Output

Print the minimal length of water pipes needed to stop water supply in a line.
The absolute or relative error should be less than or 10-6.
When you cannot stop water supply to the repairing point even though you close all stop valves, print "-1" in a line.

Sample Input 1

1 2
0 0 10 0
1 0
9 0
0 0
5 0

Output for the Sample Input 1

9.0

Sample Input 2

5 3
0 4 2 4
0 2 2 2
0 0 2 0
0 0 0 4
2 0 2 4
0 2
1 0
2 2
1 4
2 1

Output for the Sample Input 2

3.0

Sample Input 3

2 1
0 0 0 4
0 2 2 2
1 2
0 1
0 3

Output for the Sample Input 3

-1

Solution in C++ (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

typedef complex<double> P;

const double eps = 1e-6;

bool equals(double a, double b) {
  return abs(a - b) < eps;
}

namespace std {
  bool operator < (const P &a, const P &b) {
    if(!equals(a.real(), b.real())) return a.real() < b.real();
    if(!equals(a.imag(), b.imag())) return a.imag() < b.imag();
    return false;
  }
}

double cross(P a, P b) {
  return a.real()*b.imag() - a.imag()*b.real();
}

double dot(P a, P b) {
  return a.real()*b.real() + a.imag()*b.imag();
}

bool isParallel(P a, P b) {
  return equals(cross(a, b), 0.0);
}

int ccw(P p, P a, P b) {
  a -= p;
  b -= p;
  if(cross(a, b) > eps) return 1;
  if(cross(a, b) < -eps) return -1;
  if(dot(a, b) < -eps) return 2;
  if(norm(b) - norm(a) > eps) return -2;
  return 0;
}

bool isIntersect(P a1, P a2, P b1, P b2) {
  return ( ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
	   ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0 );
}

P getCrossP(P a1, P a2, P b1, P b2) {
  P a = a2 - a1;
  P b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}

///////////////////////////////////////////////////////////////////////////

struct Edge {
  int to; double w;
};

typedef vector<vector<Edge> > Graph;

int getId(P p, map<P, int> &m) {
  if(!m.count(p)) {
    int tmp = m.size();
    m[p] = tmp;
  }
  return m[p];
}

int getId(P p, map<P, int> &m, map<int, P> &r) {
  r[getId(p, m)] = p;
  return m[p];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<P> X(N), Y(N), Z(M);
  P src, dst;
  map<P, int> id;
  map<int, P> rid;

  for(int i = 0; i < N; ++i) {
    cin >> X[i].real() >> X[i].imag() >> Y[i].real() >> Y[i].imag();
    getId(X[i], id, rid);
    getId(Y[i], id, rid);
  }
  for(int i = 0; i < M; ++i) {
    cin >> Z[i].real() >> Z[i].imag();
    getId(Z[i], id, rid);
  }
  cin >> src.real() >> src.imag();
  getId(src, id, rid);
  cin >> dst.real() >> dst.imag();
  getId(dst, id, rid);

  for(int i = 0; i < N; ++i) {
    for(int j = i+1; j < N; ++j) {
      if(isParallel(X[i] - Y[i], X[j] - Y[j])) {
	//
      } else if(isIntersect(X[i], Y[i], X[j], Y[j])) {
	P p = getCrossP(X[i], Y[i], X[j], Y[j]);
	getId(p, id, rid);
      }
    }
  }

  int V = id.size();
  Graph G(V);

  for(int i = 0; i < N; ++i) {
    set<P> s;
    vector<pair<double, P> > v;
    s.insert(X[i]);
    s.insert(Y[i]);
    v.push_back(make_pair(0.0, X[i]));
    v.push_back(make_pair(norm(Y[i] - X[i]), Y[i]));
    for(int j = i+1; j < N; ++j) {
      if(isParallel(X[i] - Y[i], X[j] - Y[j])) {
	//
      } else if(isIntersect(X[i], Y[i], X[j], Y[j])) {
	P p = getCrossP(X[i], Y[i], X[j], Y[j]);
	if(s.count(p)) continue;
	s.insert(p);
	v.push_back(make_pair(norm(p - X[i]), p));
      }
    }
    for(int j = 0; j < M; ++j) {
      if(ccw(X[i], Y[i], Z[j]) == 0 && !s.count(Z[j])) {
	s.insert(Z[j]);
	v.push_back(make_pair(norm(Z[j] - X[i]), Z[j]));
      }
    }

    if(ccw(X[i], Y[i], src) == 0 && !s.count(src)) {
      s.insert(src);
      v.push_back(make_pair(norm(src - X[i]), src));
    }
    if(ccw(X[i], Y[i], dst) == 0 && !s.count(dst)) {
      s.insert(dst);
      v.push_back(make_pair(norm(dst - X[i]), dst));
    }
    sort(v.begin(), v.end());
    for(int j = 0; j+1 < v.size(); ++j) {
      int a = getId(v[j].second, id);
      int b = getId(v[j+1].second, id);
      double w = abs(v[j].second - v[j+1].second);
      G[a].push_back((Edge){b, w});
      G[b].push_back((Edge){a, w});
    }
  }
  if(0) {
  cout << V << endl;
  for(int i = 0; i < V; ++i) {
    cout << rid[i] << ":";
    for(int j = 0; j < G[i].size(); ++j) {
      if(j) cout << ",";
      cout << rid[G[i][j].to];
    }
    cout << endl;
  }
  }

  try {
    vector<int> F(V), F2(V);
    for(int i = 0; i < M; ++i) {
      F[getId(Z[i], id)] = 1;
    }
    set<pair<int, int> > vis;
    queue<int> que;
    
    que.push(getId(dst, id));
    int isrc = getId(src, id);
    while(que.size()) {
      int v = que.front();
      que.pop();
      for(int i = 0; i < G[v].size(); ++i) {
	int nv = G[v][i].to;
	if(nv == isrc) throw 0;
	if(vis.count(make_pair(nv, v))) continue;
	vis.insert(make_pair(nv, v));
	vis.insert(make_pair(v, nv));
	if(F[nv]) {
	  F2[nv] = 1;
	  continue;
	}
	que.push(nv);
      }
    }
    double res = 0;
    que.push(isrc);
    while(que.size()) {
      int v = que.front();
      que.pop();
      for(int i = 0; i < G[v].size(); ++i) {
	int nv = G[v][i].to;
	if(vis.count(make_pair(nv, v))) continue;
	vis.insert(make_pair(nv, v));
	vis.insert(make_pair(v, nv));
	res += G[v][i].w;
	if(F2[nv]) continue;
	que.push(nv);
      }
    }
    res = -res;
    for(int i = 0; i < N; ++i) {
      res += abs(X[i] - Y[i]);
    }
    printf("%.10f\n", res);
  } catch(...) {
    printf("-1\n");
  }
  return 0;
}