Cod sursa(job #1586189)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 31 ianuarie 2016 20:53:12
Problema Portal3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 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(int currDist) {

	if (currDist >= sol)
		return;

	if (path.back() == n) {

		sol = currDist;

		return;

	}

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

		if (vis[i])
			continue;

		int nextDist = (1LL * currDist + getDist(path.back(), i) > inf ? inf : currDist + getDist(path.back(), i));

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

		back(nextDist);

		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;
		path.push_back(0);
		back(0);

		fout << sol << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!