Cod sursa(job #1673345)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 3 aprilie 2016 17:59:49
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];

int 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;
}