Pagini recente » Cod sursa (job #683361) | Cod sursa (job #2156351) | Cod sursa (job #570882) | Cod sursa (job #1292101) | Cod sursa (job #639783)
Cod sursa(job #639783)
#include <stdio.h>
#define ll long long
#define INF (1LL<<62)
int t,n,m,st[4],tip[4],actx,acty;
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;
actx=acty=0;
for (i=1; i<=k-1; i++)
{
if (!tip[i])
{
act+=(ll)modul(A[st[i]].x1-actx)+modul(A[st[i]].y1-acty)+A[st[i]].c;
actx=A[st[i]].x2; acty=A[st[i]].y2;
}
else
{
act+=(ll)modul(A[st[i]].x2-actx)+modul(A[st[i]].y2-acty)+A[st[i]].c;
actx=A[st[i]].x1; acty=A[st[i]].y1;
}
}
act+=(ll)modul(actx-n)+modul(acty-m);
rez=min(rez,act);
return ;
}
for (i=1; i<=3; i++)
if (!marc[i])
{
st[k]=i;
marc[i]=1;
tip[k]=0;
back(k+1,lim);
tip[k]=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;
}