Pagini recente » Cod sursa (job #644862) | Cod sursa (job #412334) | Cod sursa (job #2635727) | Cod sursa (job #642350) | Cod sursa (job #635805)
Cod sursa(job #635805)
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;
struct Portal {
long x,y;
long long c;
};
Portal P[10];
long n,m;
void read () {
scanf("%ld%ld",&n,&m);
scanf("%ld%ld%ld%ld%ld",&P[1].x,&P[1].y,&P[2].x,&P[2].y,&P[1].c);
P[2].c=P[1].c;
scanf("%ld%ld%ld%ld%ld",&P[3].x,&P[3].y,&P[4].x,&P[4].y,&P[3].c);
P[4].c=P[3].c;
scanf("%ld%ld%ld%ld%ld",&P[5].x,&P[5].y,&P[6].x,&P[6].y,&P[5].c);
P[6].c=P[5].c;
}
long dist (Portal A , Portal B ) {
return labs(A.x-B.x)+labs(A.y-B.y);
}
void rez () {
long i;
long long min[10];
long long mine,mins;
queue<Portal>q;
Portal final;
Portal temp,temp2;
mine=mins=min[1]=min[2]=min[3]=min[4]=min[5]=min[6]=4294967295;
temp.x=0;
temp.y=0;
temp.c=0;
final.x=n;
final.y=m;
q.push(temp);
final.c=0;
while (!q.empty()) {
temp=q.front();
for (i=1;i<=6;i++) {
if ((i&1)==0) {
temp2.x=P[i-1].x;
temp2.y=P[i-1].y;
temp2.c=temp.c+(long long)dist(temp,P[i])+P[i].c;
if (min[i-1]>temp2.c) {
min[i-1]=temp2.c;
q.push(temp2);
}
}
else {
temp2.x=P[i+1].x;
temp2.y=P[i+1].y;
temp2.c=temp.c+(long long)dist(temp,P[i])+P[i].c;
if (min[i+1]>temp2.c) {
min[i+1]=temp2.c;
q.push(temp2);
}
}
temp2.x=P[i].x;
temp2.y=P[i].y;
temp2.c=temp.c+(long long)dist(temp,P[i]);
if (temp2.c<min[i]) {
min[i]=temp2.c;
q.push(temp2);
}
}
temp2.x=n;
temp2.y=m;
temp2.c=temp.c+dist(temp,final);
if (temp2.c<mine) {
mine=temp2.c;
}
q.pop();
}
printf("%lld\n",mine);
}
int main() {
long t,i;
freopen("portal3.in","r",stdin);
freopen("portal3.out","w",stdout);
scanf("%ld",&t);
for (i=1;i<=t;i++) {
read();
rez();
}
return 0;
}