Cod sursa(job #354679)

Utilizator ProtomanAndrei Purice Protoman Data 9 octombrie 2009 00:18:08
Problema Bibel Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <algorithm>
#include <stdio.h>
#include <vector>

#define MAX 18
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

int op;
vector <pair <pair <int, int>, int> > ct;
vector <pair <int, int> > cp;
int sol[1 << MAX][MAX];

inline int min(int x, int y)
{
	if (x > y)
		return y;
	return x;
}

inline int dist(pair <int, int> p1, pair <int, int> p2)
{
	return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}

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

	ct.pb(mp(mp(0, 0), 0));
	for (scanf("%d", &op); op != 2; scanf("%d", &op))
		if (op == 1)
		{
			for (int conf = 1; conf < (1 << cp.size()); conf++)
				for (int ac = 0; ac < cp.size(); ac++)
					sol[conf][ac] = LONG_MAX;
			for (int ac = 0; ac < cp.size(); ac++)
				for (int tr = 0; tr < ct.size(); tr++)
					sol[1 << ac][ac] = min(sol[1 << ac][ac], ct[tr].y + dist(ct[tr].x, cp[ac]));
			
			for (int conf = 1; conf < (1 << cp.size()); conf++)
				for (int ac = 0; ac < cp.size(); ac++)
					if (conf & (1 << ac))
						for (int ales = 0; ales < cp.size(); ales++)
							if (!(conf & (1 << ales)))
								sol[conf | (1 << ales)][ales] = min(sol[conf | (1 << ales)][ales], sol[conf][ac] + dist(cp[ac], cp[ales]));

			int rez = LONG_MAX;
			ct.clear();
			for (int i = 0; i < cp.size(); i++)
			{
				ct.pb(mp(cp[i], sol[(1 << cp.size()) - 1][i]));
				rez = min(rez, ct[i].y);
			}

			printf("%d\n", rez);
			cp.clear();
		}
		else
		{
			int x, y;
			scanf("%d %d", &x, &y);

			cp.pb(mp(x, y));
		}

	fclose(stdin);
	fclose(stdout);
	return 0;
}