Pagini recente » Cod sursa (job #1922115) | Cod sursa (job #2563824) | Cod sursa (job #10313) | Cod sursa (job #99157) | Cod sursa (job #636641)
Cod sursa(job #636641)
#include <stdio.h>
#define ll long long
int t,n,m,st[4];
ll act,rez;
struct portal{int x1,y1,x2,y2,c;};
portal A[4];
char marc[4];
inline int modul(int x)
{
return x<0 ? -x : x;
}
inline ll min(ll x,ll y)
{
return x<y ? x : y;
}
void back(int k,int lim)
{
int i;
if (k==lim+1)
{
act=0;
for (i=1; i<=k-1; i++)
act+=(ll)modul(A[st[i-1]].x2-A[st[i]].x1)+modul(A[st[i-1]].y2-A[st[i]].y1)+A[st[i]].c;
act+=(ll)modul(A[st[k-1]].x2-n)+modul(A[st[k-1]].y2-m);
rez=min(rez,act);
return ;
}
for (i=1; i<=3; i++)
if (!marc[i])
{
st[k]=i;
marc[i]=1;
back(k+1,lim);
marc[i]=0;
}
}
int main()
{
freopen("portal3.in","r",stdin);
freopen("portal3.out","w",stdout);
scanf("%d",&t);
int i;
while (t--)
{
scanf("%d%d",&n,&m);
for (i=1; i<=3; i++)
scanf("%d%d%d%d%d",&A[i].x1,&A[i].y1,&A[i].x2,&A[i].y2,&A[i].c);
rez=n+m;
back(1,1);
back(1,2);
back(1,3);
printf("%lld\n",rez);
}
return 0;
}