Pagini recente » Cod sursa (job #43481) | clasament-arhiva-educationala | Cod sursa (job #2319354) | Cod sursa (job #2853305) | Cod sursa (job #1074382)
#include <fstream>
#define PII pair<long long, long long>
#define x first
#define y second
using namespace std;
ifstream fin("portal3.in");
ofstream fout("portal3.out");
PII a[4], b[4], c[4];
long long cost, d[4], v[4], n, m, nr, sol;
long long modul(long long x)
{
if(x<0) return -x;
return x;
}
void bt(int k)
{
cost+=n-c[nr].x+m-c[nr].y;
if(cost<sol) sol=cost;
cost-=n-c[nr].x+m-c[nr].y;
if(k==4) return;
int i;
for(i=1;i<4;i++)
{
if(!v[i])
{
c[++nr]=a[i];
v[i]=1;
cost+=d[i];
cost+=modul(b[i].x-c[nr-1].x)+modul(b[i].y)-c[nr-1].y;
bt(k+1);
cost-=modul(b[i].x-c[nr-1].x)+modul(b[i].y)-c[nr-1].y;
c[nr]=b[i];
cost+=modul(a[i].x-c[nr-1].x)+modul(a[i].y)-c[nr-1].y;
bt(k+1);
cost-=modul(a[i].x-c[nr-1].x)+modul(a[i].y)-c[nr-1].y;
cost-=d[i];
v[i]=0;
--nr;
}
}
}
int main()
{
int t, i;
fin>>t;
while(t--)
{
sol=0x3f3f3f3f;
fin>>n>>m;
for(i=1;i<4;i++)
{
fin>>a[i].x>>a[i].y>>b[i].x>>b[i].y>>d[i];
}
bt(1);
fout<<sol<<"\n";
}
fin.close();
fout.close();
}