Cod sursa(job #1586184)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 31 ianuarie 2016 20:49:15
Problema Portal3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

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

const int n = 7, inf = 2000000001;

vector< pair<int, int> > v;
vector<int> path;

int dist[8][8], sol;

inline int modul(int a) {

	return (a >= 0 ? a : -a);

}

inline int getDist(int a, int b) {

	if (dist[a][b] != -1)
		return dist[a][b];

	return modul(v[a].first - v[b].first) + modul(v[a].second - v[b].second);

}

bool vis[8];

void back(void) {

	if (!path.empty() && path.back() == n) {

		long long currDist = getDist(0, path[0]);

		for (int i = 1; i < (int)path.size(); ++i)
			currDist += getDist(path[i - 1], path[i]);

		sol = min(sol, (currDist >= inf ? inf : (int)currDist));

		return;

	}

	for (int i = 1; i <= n; ++i) {

		if (vis[i])
			continue;

		vis[i] = true;
		path.push_back(i);

		back();

		vis[i] = false;
		path.pop_back();

	}

}

int main() {

	v.resize(8);

	int testCount;
	fin >> testCount;

	while (testCount--) {

		memset(dist, -1, sizeof dist);

		fin >> v[n].first >> v[n].second;
		
		for (int i = 1; i < n; i += 2) {

			fin >> v[i].first >> v[i].second;
			fin >> v[i + 1].first >> v[i + 1].second;

			int cost;
			fin >> cost;

			dist[i][i + 1] = dist[i + 1][i] = cost;

		}

		sol = inf;
		back();

		fout << sol << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!