Mai intai trebuie sa te autentifici.

Cod sursa(job #638693)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 21 noiembrie 2011 13:51:17
Problema Portal3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.02 kb
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;

#define LL long long
#define inf (1LL << 60)

LL sol;

LL abss (LL A)
{
	if (A > 0) return A;
	else return - A;
}

LL dist (LL x1, LL y1, LL x2, LL y2)
{
	return abss (x1 - x2) + (LL) abss (y1 - y2);
}


void solve (LL x1, LL y1, LL x2, LL y2, LL x3, LL y3, LL x4, LL y4, LL x5, LL y5, LL x6, LL y6, LL c1, LL c2, LL c3, LL N, LL M)
{
	LL p1 = dist (0, 0, N, M);
	LL p2 = dist (0, 0, x1, y1) + c1 + dist (x2, y2, N, M);
	LL p3 = dist (0, 0, x3, y3) + c2 + dist (x4, y4, N, M);
	LL p4 = dist (0, 0, x5, y5) + c3 + dist (x6, y6, N, M);
	LL p5 = dist (0, 0, x1, y1) + c1 + dist (x2, y2, x3, y3) + c2 + dist (x4, y4, N, M);
	LL p9 = dist (0, 0, x3, y3) + c2 + dist (x4, y4, x1, y1) + c1 + dist (x2, y2, N, M);
	
	LL p6 = dist (0, 0, x1, y1) + c1 + dist (x2, y2, x5, y5) + c3 + dist (x6, y6, N, M);
	LL p10 = dist (0, 0, x5, y5) + c3 + dist (x6, y6, x1, y1) + c1 + dist (x2, y2, N, M);
	
	LL p7 = dist (0, 0, x3, y3) + c2 + dist (x4, y4, x5, y5) + c3 + dist (x6, y6, N, M);
	LL p11 = dist (0, 0, x5, y5) + c3 + dist (x6, y6, x3, y3) + c2 + dist (x4, y4, N, M);
	
	LL p8 = dist (0, 0, x1, y1) + c1 + dist (x2, y2, x3, y3) + c2 + dist (x4, y4, x5, y5) + c3 + dist (x6, y6, N, M);
	LL p12 = dist (0, 0, x1, y1) + c1 + dist (x2, y2, x5, y5) + c3 + dist (x6, y6, x3, y3) + c2 + dist (x4, y4, N, M);
	LL p13 = dist (0, 0, x3 ,y3) + c2 + dist (x4 ,y4 ,x1 ,y1) + c1 + dist (x2 ,y2 ,x5 ,y5) + c3 + dist (x6 ,y6 ,N ,M);
	LL p14 = dist (0, 0, x3, y3) + c2 + dist (x4, y4, x5, y5) + c3 + dist (x6, y6, x1, y1) + c1 + dist (x2, y2, N, M);
	LL p15 = dist (0, 0, x5, y5) + c3 + dist (x6, y6, x1, y1) + c1 + dist (x2, y2, x3, y3) + c2 + dist (x4, y4, N, M);
	LL p16 = dist (0, 0, x5, y5) + c3 + dist (x6, y6, x3, y3) + c2 + dist (x4, y4, x1, y1) + c1 + dist (x2, y2, N, M);
	
	sol = min (sol, p1);
	sol = min (sol, p2);
	sol = min (sol, p3);
	sol = min (sol, p4);
	sol = min (sol, p5);
	sol = min (sol, p6);
	sol = min (sol, p7);
	sol = min (sol, p8);
	sol = min (sol, p9);
	sol = min (sol, p10);
	sol = min (sol, p11);
	sol = min (sol, p12);
	sol = min (sol, p13);
	sol = min (sol, p14);
	sol = min (sol, p15);
	sol = min (sol, p16);
}

int main()
{
	freopen ("portal3.in", "r", stdin);
	freopen ("portal3.out", "w", stdout);
	
	int T;
	
	scanf ("%d", &T);
	
	while (T --)
	{
		LL N, M;
		
		scanf ("%lld %lld", &N, &M);
		
		LL x1, y1, x2, y2, c1;
		LL x3, y3, x4, y4, c2;
		LL x5, y5, x6, y6, c3;
		
		scanf ("%lld %lld %lld %lld %lld", &x1, &y1, &x2, &y2, &c1);
		scanf ("%lld %lld %lld %lld %lld", &x3, &y3, &x4, &y4, &c2);
		scanf ("%lld %lld %lld %lld %lld", &x5, &y5, &x6, &y6, &c3);
		
		
		sol = inf;
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // -> -> ->
		
		swap (x1, x2);
		swap (y1, y2);
		swap (x3, x4);
		swap (y3, y4);
		swap (x5, x6);
		swap (y5, y6);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // <- <- <-
		
		swap (x1, x2);
		swap (y1, y2);
		swap (x3, x4);
		swap (y3, y4); 		
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // -> -> <-
		
		swap (x3, x4);
		swap (y3, y4);
		swap (x5, x6);
		swap (y5, y6);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // -> <- ->
		
		swap (x1, x2);
		swap (y1, y2);
		swap (x3, x4);
		swap (y3, y4);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // <- -> ->
		
		swap (x3, x4);
		swap (y3, y4);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // <- <- ->
		
		swap (x3, x4);
		swap (y3, y4);
		swap (x5, x6);
		swap (y5, y6);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // <- -> <-
		
		swap (x1, x2);
		swap (y1, y2);
		swap (x3, x4);
		swap (y3, y4);
		
		solve (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, c1, c2, c3, N, M); // -> <- <-
		
		
		printf ("%lld\n", sol);
	}
	
	return 0;
}