Cod sursa(job #982518)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 9 august 2013 13:42:37
Problema Portal3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
using namespace std;
#include <fstream>
#include <iostream>
#include <climits>
#include <cstdlib>

int x[4],n,m,uz[4];
int T,px[4][2],py[4][2],c[4];

long long sol;

void Back(int k){
    for(int i=1;i<=3;++i)
        if(uz[i]==0){
            uz[i]=1;
            for(int a=-1 ; a<=1 ; a++){
                x[k] = a*i;
                if(k==3){
                    long long int S = 0;
                    int ii=0,jj=0, start, stop;
                    for(int j=1 ; j<=3 ; j++)
                    if(x[j])
                    {
                        start=0,stop=1;
                        if(x[j]<0)
                            start = 1, stop = 0;
                        int xj = abs(x[j]);
                        S += abs(ii-px[xj][start]) + abs(jj-py[xj][start])+c[xj];
                        ii = px[xj][stop], jj = py[xj][stop];
                    }
                    S+= abs(ii-n)+ abs(jj-m);
                    if(S<sol)
                        sol=S;
                }
                else
                    Back(k+1);
            }
            uz[i]=0;
        }
}

int main(){
    ifstream fin("portal3.in");
    ofstream fout("portal3.out");
    fin>>T;
    for(; T ;--T){
        fin>>n>>m;
        fin >> px[1][0] >> py[1][0] >> px[1][1] >> py[1][1] >> c[1];
        fin >> px[2][0] >> py[2][0] >> px[2][1] >> py[2][1] >> c[2];
        fin >> px[3][0] >> py[3][0] >> px[3][1] >> py[3][1] >> c[3];
        uz[1] = uz[2] = uz[3] = 0;
        sol = 10ll*INT_MAX;
        Back(1);
        fout << sol << "\n";
    }
    return 0;
}