Pagini recente » Cod sursa (job #1353847) | Cod sursa (job #3178300) | Cod sursa (job #1716728) | Cod sursa (job #824569) | Cod sursa (job #1586189)
#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!