Cod sursa(job #636010)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 19 noiembrie 2011 16:17:14
Problema Portal3 Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 0.89 kb
#include <cstdio>
#include <cstdlib>
#define inf 1000000000000LL

int t, n, m, x[11], y[11], d[11][11], u[11], v[11];
long long sol;

void back(int p)
{
	int i;
	long long dist=0;
	v[p]=7;
	for (i=0; i<p; i++)
		dist+=d[v[i]][v[i+1]];
	if (dist<sol) sol=dist;
	if (p<=6)
		for (i=1; i<=6; i++)
			if (!u[i])
			{
				u[i]=1;
				v[p]=i;
				back(p+1);
				u[i]=0;
			}
}

int main()
{
	freopen("portal3.in","r",stdin);
	freopen("portal3.out","w",stdout);
	scanf("%d", &t);
	int i, j, c;
	while (t--)
	{
		scanf("%d %d", &n, &m);
		for (i=1; i<=5; i+=2) 
		{
			scanf("%d %d %d %d %d", &x[i], &y[i], &x[i+1], &y[i+1], &c);
			d[i][i+1]=d[i+1][i]=c;
		}
		x[7]=n;
		y[7]=m;
		for (i=0; i<=7; i++)
			for (j=0; j<=7; j++)
				if (!d[i][j]) 
					d[i][j]=abs(x[i]-x[j])+abs(y[i]-y[j]);
		sol=inf;
		for (i=1; i<=6; i++) u[i]=0;
		back(1);
		printf("%lld\n",sol);
	}
}