Pagini recente » Cod sursa (job #838290) | Cod sursa (job #1591218) | Cod sursa (job #676175) | Cod sursa (job #2317406) | Cod sursa (job #636671)
Cod sursa(job #636671)
#include <stdio.h>
#define ll long long
#define INF (1LL<<62)
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=INF;
back(1,1);
back(1,2);
back(1,3);
printf("%lld\n",rez);
}
return 0;
}