Problem: Example

Input

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


Output

6.0000000000000000

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using point = complex<ld>;

const ld eps = 1e-10;
const ld INF = 1e9;

struct edge {
	int to, cap;
	ld cost;
	int rev;
	edge(int to_, int cap_, ld cost_, int rev_)
		: to(to_), cap(cap_), cost(cost_), rev(rev_) {}
};

const int MAX = 10000;

int V;

vector<edge> G[MAX];
ld h[MAX], dist[MAX];
int prevv[MAX], preve[MAX];

void add(int from, int to, int cap, ld cost) {
	G[from].emplace_back(to, cap, cost, G[to].size());
	G[to].emplace_back(from, 0, -cost, G[from].size() - 1);
}

ld calc(int s, int t, int f) {
	using pii = pair<ld, int>;
	ld res = 0;
	fill(h, h + V, 0);
	while (f > 0) {
		priority_queue<pii, vector<pii>, greater<pii>> que;
		fill(dist, dist + V, INF);
		dist[s] = 0;
		que.emplace(0, s);
		while (!que.empty()) {
			pii p = que.top(); que.pop();
			int v = p.second;
			if (dist[v] < p.first) continue;
			for (size_t i = 0; i < G[v].size(); i++) {
				edge &e = G[v][i];
				if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] + eps) {
					dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					que.emplace(dist[e.to], e.to);
				}
			}
		}
		assert(dist[t] < INF / 2);
		for (int v = 0; v < V; v++)
			h[v] += dist[v];
		int d = f;
		for (int v = t; v != s; v = prevv[v]) {
			d = min(d, G[prevv[v]][preve[v]].cap);
		}
		f -= d;
		res += d * h[t];
		for (int v = t; v != s; v = prevv[v]) {
			edge &e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	cout << fixed << setprecision(10);
	int n;
	cin >> n;
	int sx, sy;
	cin >> sx >> sy;
	point s(sx, sy);
	ld res = 0;
	vector<point> f(n), t(n);
	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		f[i] = point(x, y);
		cin >> x >> y;
		t[i] = point(x, y);
		res += abs(f[i] - t[i]);
	}
	V = n * 2 + 2;
	int src = n * 2, sink = n * 2 + 1;
	for (int i = 0; i < n; i++) {
		add(src, n + i, 1, 0);
		add(i, sink, 1, 0);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			add(n + i, j, 1, abs(t[i] - f[j]));
		}
	}
	res += calc(src, sink, n);
	cout << res << endl;
	return 0;
}