Pagini recente » Cod sursa (job #3274953) | Cod sursa (job #107560) | Cod sursa (job #485049) | Cod sursa (job #164838) | Cod sursa (job #972034)
Cod sursa(job #972034)
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;
}