Cod sursa(job #709826)

Utilizator mottyMatei-Dan Epure motty Data 8 martie 2012 17:07:00
Problema Portal3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <fstream>
#include <cstdlib>

using namespace std;

ifstream in("portal3.in");
ofstream out("portal3.out");

struct point {
	int x;
	int y;
};

struct portal {
	point a;
	point b;
	long long cost;
};

const int N = 5;

long long best;

point start;
point end;

int port[N];
int st[N];

portal v[N];

inline int getDist(point a, point b)
{
	return abs(b.x - a.x) + abs(b.y - a.y);// - 1;
}

void readCase()
{
	in >> end.x >> end.y;
	best = getDist(start, end);

	for (int i = 1; i <= 3; ++i)
		in >> v[i].a.x >> v[i].a.y >> v[i].b.x >> v[i].b.y >> v[i].cost;
}

long long checkCost(int lastPoz)
{
	long long curCost = 0;

	point lastPoint = start;

	for (int i = 1; i < lastPoz; ++i) {
		point curPoint;

		if (st[i] == 0) {
			curPoint = v[port[i]].a;
			curCost += getDist(lastPoint, curPoint) + v[port[i]].cost;
			lastPoint = v[port[i]].b;
		}
		else {
			curPoint = v[port[i]].b;
			curCost += getDist(lastPoint, curPoint) + v[port[i]].cost;
			lastPoint = v[port[i]].a;
		}
	}

	curCost += getDist(lastPoint, end);

	return curCost;
}

void arrangePortals(int poz)
{
	long long curCost = checkCost(poz);
	if (curCost < best)
		best = curCost;

	if (poz == 4)
		return;

	for (int i = 1; i <= 3; ++i) {
		bool check = false;

		for (int k = 1; k < poz; ++k)
			if (port[k] == i)
				check = true;

		if (!check) {
			port[poz] = i;

			for (int j = 0; j <= 1; ++j) {
				st[poz] = j;
				arrangePortals(poz+1);
			}
		}
	}
}

long long solveCase()
{
	readCase();
	arrangePortals(1);

	return best;
}

int main()
{
	int t;
	in >> t;

	while (t--)
		out << solveCase() << "\n";

	return 0;
}