Pagini recente » Cod sursa (job #438432) | Cod sursa (job #443040) | Cod sursa (job #173785) | Cod sursa (job #148775) | Cod sursa (job #1134237)
#include <cstdio>
#include <algorithm>
#define INF 2000000000
using namespace std;
int st[10],st1[10],len,N,M,sol;
struct portal
{
int x1,y1,x2,y2,c;
};
portal p[5];
inline int Modul(int x)
{
if(x<0)
return -x;
return x;
}
inline bool Valid(int top)
{
int i;
for(i=1;i<top;++i)
if(st[i]==st[top])
return false;
return true;
}
inline void Solve1()
{
int lin=0,col=0,cnt=0,i;
for(i=1;i<=len;++i)
{
if(st1[i]==1)
{
cnt+=Modul(p[st[i]].x1-lin)+Modul(p[st[i]].y1-col)+p[st[i]].c;
lin=p[st[i]].x2; col=p[st[i]].y2;
}
else
{
cnt+=Modul(p[st[i]].x2-lin)+Modul(p[st[i]].y2-col)+p[st[i]].c;
lin=p[st[i]].x1; col=p[st[i]].y1;
}
}
cnt+=Modul(N-lin)+Modul(M-col);
sol=min(sol,cnt);
}
inline void Solve()
{
int top1;
bool cand1;
top1=1; st1[1]=0;
while(top1)
{
cand1=false;
while(!cand1 && st1[top1]<2)
{
++st1[top1];
cand1=true;
}
if(!cand1)
--top1;
else
if(top1==len)
Solve1();
else
st1[++top1]=0;
}
}
int main()
{
int T,i,top;
bool cand;
freopen ("portal3.in","r",stdin);
freopen ("portal3.out","w",stdout);
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N,&M);
for(i=1;i<=3;++i)
scanf("%d%d%d%d%d", &p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2,&p[i].c);
sol=N+M;
for(len=1;len<=3;++len)
{
top=1; st[top]=0;
while(top)
{
cand=false;
while(!cand && st[top]<3)
{
++st[top];
cand=Valid(top);
}
if(!cand)
--top;
else
if(top==len)
Solve();
else
st[++top]=0;
}
}
printf("%d\n", sol);
}
return 0;
}