Mai intai trebuie sa te autentifici.
Cod sursa(job #638891)
Utilizator | Data | 21 noiembrie 2011 20:30:10 | |
---|---|---|---|
Problema | Portal3 | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 1.24 kb |
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
ifstream in("portal3.in");
ofstream out("portal3.out");
const int N = 5;
int n, m;
int a[N], b[N];
int pa[N], pb[N], pc[N];
int c[N][N];
int sol[N];
bool used[N];
int best = 0x3f3f3f3f;
void Read()
{
in >> n >> m;
a[4] = n, b[4] = m;
for (int i = 1; i <= 3; ++i)
in >> pa[i] >> pb[i] >> a[i] >> b[i] >> pc[i];
}
void GetCosts()
{
for (int i = 1; i <= 4; ++i)
for (int j = 0; j <= 3; ++j)
{
int v1, v2 = 0x3f3f3f3f;
v1 = abs(a[i] - a[j]) + abs(b[i] - b[j]);
if (i != 4)
v2 = abs(pa[i] - a[j]) + abs(pb[i] - b[j]) + pc[i];
c[j][i] = (v1<v2 ? v1:v2);
}
}
void Check(int pos)
{
int rez = 0;
for (int i = 1; i < pos; ++i)
rez += c[sol[i-1]][sol[i]];
if (rez < best)
best = rez;
}
void Back(int pos)
{
if (used[4])
{
Check(pos);
return;
}
for (int i = 1; i <= 4; ++i)
if (!used[i])
{
sol[pos] = i;
used[i] = true;
Back(pos+1);
used[i] = false;
}
}
int Solve()
{
Read();
GetCosts();
memset(used, 0, sizeof(used));
Back(1);
return best;
}
int main()
{
int t;
in >> t;
while (t--)
out << Solve() << "\n";
return 0;
}