Cod sursa(job #343011)

Utilizator CezarMocanCezar Mocan CezarMocan Data 24 august 2009 17:02:50
Problema Adapost Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.42 kb
#include <cstdio>
#include <vector>
#include <math.h>
#include <cstring>
#include <cassert>
#include <algorithm>
#define maxn 810
#define inf 0x3f3f3f3f

using namespace std;

struct punct {
	int x, y;
};

int n, i, j;
double aa, bb;
int g[maxn][maxn], cost[maxn][maxn];
int nrg[maxn];
char f[20][maxn][maxn];
bool c[20][maxn][maxn];
int dist[maxn][maxn];
int dmin[maxn];
short int up[maxn];
punct v[maxn];
int dmax, sum;
int dst_min, cst_min;
short int src, dst;
bool ok, viz[maxn];
int heap[2 * maxn], heap_sz, poz_heap[maxn];
short int q[maxn * maxn];
int cb[maxn * maxn], nr;
int indice;

inline int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

inline int distanta(punct a, punct b) {
	return (int) sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y));
}

inline void fill(int v[], int val) {
	int i;
	for (i = 0; i < dst + 10; i++)
		v[i] = val;
}

inline void build_graf(int dmax) {
	int i;

	memset(nrg, 0, sizeof(nrg));

	for (i = 1; i <= n; i++) {
		nrg[i]++;
		g[i][nrg[i]] = src; cost[i][nrg[i]] = 0;
		nrg[src]++;
		g[src][nrg[src]] = i; cost[src][nrg[src]] = 0;
		c[indice][src][i] = 1; 
		f[indice][src][i] = f[indice][i][src] = 0;
	}

	for (i = n + 1; i <= 2 * n; i++) {
		nrg[i]++;
		g[i][nrg[i]] = dst; cost[i][nrg[i]] = 0;
		nrg[dst]++;
		g[dst][nrg[dst]] = i; cost[dst][nrg[dst]] = 0;
		c[indice][i][dst] = 1;
		f[indice][i][dst] = f[indice][dst][i] = 0;
	}

/*	for (i = 1; i <= n; i++)
		for (j = n + 1; j <= 2 * n; j++) 
			if (dist[i][j] <= dmax) {
				nrg[i]++;
				g[i][nrg[i]] = j;
				nrg[j]++;
				g[j][nrg[j]] = i;
				cost[i][nrg[i]] = (dist[i][j]);
				cost[j][nrg[j]] = (-dist[i][j]);
				c[indice][i][j] = 1;
			}*/
		
}

void bellman_ford() {
	int p, u;
	int nod, fiu, i;

	p = u = 1;
	q[1] = src;
	viz[src] = 1;
	dmin[src] = 0;

	while (p <= u) {
		nod = q[p];
		viz[nod] = 0;
		for (i = 1; i <= nrg[nod]; i++) if (nod < g[nod][i]) {
			fiu = g[nod][i];
			if (dmin[fiu] > dmin[nod] + cost[nod][i]) {
				dmin[fiu] = dmin[nod] + cost[nod][i];
				if (viz[fiu] == 0) {
					u++;
					q[u] = fiu;
					viz[fiu] = 1;
				}
			}
		}
		p++;
	}

	sum = dmin[dst];

}

inline void init() {
//	memset(up, -1, sizeof(up));
	memset(dmin, 0x3f, sizeof(dmin));
	dmin[src] = 0;
	ok = 0;
}

inline void swap(int &a, int &b) {
	int aux;
	aux = a; a = b; b = aux;
}

inline void heap_up(int no) {
	int nod = no, ss;
	while (nod > 1) {
		ss = nod / 2;
		if (dmin[heap[nod]] < dmin[heap[ss]]) {
			swap(heap[nod], heap[ss]);
			poz_heap[heap[nod]] = nod;
			poz_heap[heap[ss]] = ss;
			nod = ss;
		}
		else
			break;
	}
}

inline void heap_down(int no) {
	int nod = no, l, r;
	while (2 * nod <= heap_sz) {
		l = 2 * nod; r = 2 * nod + 1;
		if (dmin[heap[nod]] > min(dmin[heap[l]], dmin[heap[r]])) {
			if (dmin[heap[l]] < dmin[heap[r]]) {
				swap(heap[nod], heap[l]);
				poz_heap[heap[nod]] = nod;
				poz_heap[heap[l]] = l;
				nod = l;
			}
			else {
				swap(heap[nod], heap[r]);
				poz_heap[heap[nod]] = nod;
				poz_heap[heap[r]] = r;
				nod = r;
			}
		}
		else
			break;
	}
}

inline int dijkstra() {
	int i, j, nod, fiu, fmin;

	for (i = src; i <= dst; i++)
		for (j = 1; j <= nrg[i]; j++) {
			fiu = g[i][j];
			if (dmin[i] != inf && dmin[fiu] != inf)
				cost[i][j] += dmin[i] - dmin[fiu];
		}

	init();

	for (i = src; i <= dst; i++) {
		heap[i + 1] = i;
		poz_heap[i] = i + 1;
	}

	heap_sz = dst + 1;
	heap[heap_sz + 1] = heap[heap_sz + 2] = dst + 1;

	while (heap_sz > 1 && dmin[heap[1]] != inf) {
		nod = heap[1];
		int gs = nrg[nod];
		for (i = 1; i <= gs; i++) {
			fiu = g[nod][i];
			if (f[indice][nod][fiu] < c[indice][nod][fiu] && dmin[nod] + cost[nod][i] < dmin[fiu]) {
				dmin[fiu] = dmin[nod] + cost[nod][i];
				up[fiu] = nod;
				heap_up(poz_heap[fiu]);
			}
		}

		heap[1] = heap[heap_sz];
		heap[heap_sz] = dst + 1;
		heap_sz--;
		heap_down(1);
	}

	if (dmin[dst] == inf) 
		return 0;

	ok = 1;

	fmin = 1;
/*	for (i = dst; i != src; i = up[i]) 
		fmin = min(fmin, c[up[i]][i] - f[up[i]][i]);*/

	for (i = dst; i != src; i = up[i]) {
		f[indice][up[i]][i] += fmin;
		f[indice][i][up[i]] -= fmin;
	}

	sum += dmin[dst];
	return (sum * fmin);
}

inline int posibil(int dist_max) {
	int nrp, cost;
	build_graf(dist_max);

	dmin[src] = 0;
	bellman_ford();

	ok = 1; nrp = 0; cost = 0;
	while (ok) {
		cost += dijkstra();
		nrp++;
	}

	if (nrp > n)
		return cost;
	else
		return -1;
}

inline void bsearch(int left, int right) {
	int m, p;
	while (left <= right) {
		m = (left + right) / 2;
		p = posibil(cb[m]);
		if (p != -1) {
			if (cb[m] < dst_min) {
				dst_min = cb[m];
				cst_min = p;
			}
			right = m - 1;
		}
		else
			left = m + 1;
		indice++;
	}
}

inline void init_src_dst() {
	src = 0; dst = 2 * n + 1;
}

int main() {
	freopen("adapost.in", "r", stdin);
	freopen("adapost.out", "w", stdout);

	scanf("%d", &n);
	for (i = 1; i <= 2 * n; i++) {
		scanf("%lf%lf", &aa, &bb);
		v[i].x = (int)(aa * 100000);
		v[i].y = (int)(bb * 100000);
	}

	for (i = 1; i <= n; i++) 
		for (j = n + 1; j <= 2 * n; j++) {
			dist[i][j] = dist[j][i] = distanta(v[i], v[j]);
			nr++;
			cb[nr] = dist[i][j];
			dmax = max(dmax, dist[i][j]);
		}

	sort(cb + 1, cb + nr + 1);

	dst_min = inf; cst_min = inf;
	init_src_dst();
	bsearch(1, nr);

	printf("%d.", dst_min / 100000);
	int aux = dst_min % 100000;
	if (aux)
		while (aux < 10000) {
			printf("0");
			aux *= 10;
		}
	printf("%d ", dst_min % 100000);

	printf("%d.", cst_min / 100000);
	aux = cst_min % 100000;
	if (aux)
		while (aux < 10000) {
			printf("0");
			aux *= 10;
		}
	printf("%d ", cst_min % 100000);

	return 0;
}