Cod sursa(job #1673348)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 3 aprilie 2016 18:01:39
Problema Portal3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

long long best;
int X1[5], Y1[5], X2[5], Y2[5], c[5];

long long getd(int i, int j) {
   if(i == -1 || j == -1) return 0;
   return abs(X2[i] - X1[j]) + abs(Y2[i] - Y1[j]) + c[j];
}

void upd_best(int i1, int i2 = -1, int i3 = -1, int i4 = -1) {
   long long cost = getd(0, i1) + getd(i1, i2) + getd(i2, i3) + getd(i3, i4);
   best = min(best, cost);
}

int main() {
   int t, i, n, m;

   in >> t;
   while(t--) {
      in >> n >> m;
      X1[4] = X2[4] = n;
      Y1[4] = Y2[4] = m;
      in >> X1[1] >> Y1[1] >> X2[1] >> Y2[1] >> c[1];
      in >> X1[2] >> Y1[2] >> X2[2] >> Y2[2] >> c[2];
      in >> X1[3] >> Y1[3] >> X2[3] >> Y2[3] >> c[3];
      best = 0x7fffffffffffffff;
      upd_best(4);
      upd_best(1, 4);
      upd_best(2, 4);
      upd_best(3, 4);
      upd_best(1, 2, 4);
      upd_best(1, 3, 4);
      upd_best(2, 1, 4);
      upd_best(2, 3, 4);
      upd_best(3, 1, 4);
      upd_best(3, 2, 4);
      upd_best(1, 2, 3, 4);
      upd_best(1, 3, 2, 4);
      upd_best(2, 1, 3, 4);
      upd_best(2, 3, 1, 4);
      upd_best(3, 1, 2, 4);
      upd_best(3, 2, 1, 4);
      out << best << '\n';
   }
   return 0;
}