Cod sursa(job #1586161)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 31 ianuarie 2016 20:31:37
Problema Portal3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 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);

}

void back(int curr) {

	if (curr == n) {

		path.push_back(n);

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

		path.pop_back();

		sol = min(sol, currDist);

		return;

	}

	back(curr + 1);

	path.push_back(curr);
	back(curr + 1);
	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(1);

		fout << sol << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!