Cod sursa(job #675552)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 7 februarie 2012 19:01:06
Problema Portal3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int n, m, t;
int ax[4], ay[4], bx[4], by[4], c[4], f[4];

int ab(int a)
{
    if(a<0)
        return -a;
    return a;
}

int dist(int a, int b, int c, int d)
{
    return ab(a-c)+ab(b-d);
}

long long back(long long tc, int xc, int yc)
{
    long long sol = tc + dist(xc, yc, n, m);
    long long newt;

    for(int i=0; i<3; ++i)
    {
        if(f[i])
            continue;
        f[i]=1;
        newt = (tc + dist(xc, yc, ax[i], ay[i]) + c[i]);
        sol=min(sol, back(newt, bx[i], by[i]));
        newt = (tc + dist(xc, yc, bx[i], by[i]) + c[i]);
        sol=min(sol, back(newt, ax[i], ay[i]));
        f[i]=0;
    }

    return sol;
}

int main()
{
    freopen("portal3.in", "r", stdin);
    freopen("portal3.out", "w", stdout);

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        for(int i=0; i<3; ++i)
            scanf("%d%d%d%d%d", &ax[i], &ay[i], &bx[i], &by[i], &c[i]);
        printf("%lld\n", back(0, 0, 0));
    }

    return 0;
}