Cod sursa(job #635405)

Utilizator crushackPopescu Silviu crushack Data 19 noiembrie 2011 11:12:03
Problema Portal3 Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.09 kb
#include <fstream>
#include <algorithm>
#include <stdlib.h>
using namespace std;

ifstream fin("portal3.in"); ofstream fout("portal3.out");
typedef long long ll;

char s[][10] = { "04" , "014" , "024" , "034" , "0124" , "0214" , "0134" , "0314" , "0234" , "0324" , "01234" ,
                 "01324" , "02134" , "02314" , "03124" , "03214" };

int Tes;
ll N,M;
ll X[6] , Y[6] , C[3];


ll dist(ll x0,ll y0,ll x1,ll y1){
	return abs(x0-x1)+abs(y0-y1);
}

int main()
{
	int L= sizeof(s)/sizeof(s[0]);
	fin>>Tes;
	while (Tes--)
	{
		long long Rez=(1LL<<60LL),d, x,y;int i,j;
		fin>>N>>M;
		fin>>X[0]>>Y[0]>>X[1]>>Y[1]>>C[0];
		fin>>X[2]>>Y[2]>>X[3]>>Y[3]>>C[1];
		fin>>X[4]>>Y[4]>>X[5]>>Y[5]>>C[2];
		
		for (i=0;i<L;++i)
		{
			x=y=d=0;
			for (j=1;s[i][j];++j)
			{
				if (s[i][j]=='4')
					d+=dist(x,y,N,M);
				if (s[i][j]=='1')
					d+=dist(x,y,X[0],Y[0])+C[0],x=X[1],y=Y[1];
				if (s[i][j]=='2')
					d+=dist(x,y,X[2],Y[2])+C[1],x=X[3],y=Y[3];
				if (s[i][j]=='3')
					d+=dist(x,y,X[4],Y[4])+C[2],x=X[5],y=Y[5];
			}
			Rez=min(Rez,d);
		}
		
		fout<<Rez<<"\n";
	}
	fout.close();fin.close();
	return 0;
}