Cod sursa(job #635778)

Utilizator crushackPopescu Silviu crushack Data 19 noiembrie 2011 14:51:17
Problema Portal3 Scor 100
Compilator cpp Status done
Runda .com 2011 Marime 1.96 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", "0-14", "024", "0-24", "034", "0-34", "0124", "01-24", "0-124", "0-1-24", "0214", "02-14", "0-214", "0-2-14", "0134", "01-34", "0-134", "0-1-34", "0314", "03-14", "0-314", "0-3-14", "0234", "02-34", "0-234", "0-2-34", "0324", "03-24", "0-324", "0-3-24", "01234", "012-34", "01-234", "01-2-34", "0-1234", "0-12-34", "0-1-234", "0-1-2-34", "01324", "013-24", "01-324", "01-3-24", "0-1324", "0-13-24", "0-1-324", "0-1-3-24", "02134", "021-34", "02-134", "02-1-34", "0-2134", "0-21-34", "0-2-134", "0-2-1-34", "02314", "023-14", "02-314", "02-3-14", "0-2314", "0-23-14", "0-2-314", "0-2-3-14", "03124", "031-24", "03-124", "03-1-24", "0-3124", "0-31-24", "0-3-124", "0-3-1-24", "03214", "032-14", "03-214", "03-2-14", "0-3214", "0-32-14", "0-3-214", "0-3-2-14"
		};


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]=='-')
				{
					++j;
					if (s[i][j]=='1')
						d+=dist(x,y,X[1],Y[1])+C[0],x=X[0],y=Y[0];
					if (s[i][j]=='2')
						d+=dist(x,y,X[3],Y[3])+C[1],x=X[2],y=Y[2];
					if (s[i][j]=='3')
						d+=dist(x,y,X[5],Y[5])+C[2],x=X[4],y=Y[4];
					continue;
				}
			
				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;
}