Cod sursa(job #638223)

Utilizator andunhillMacarescu Sebastian andunhill Data 20 noiembrie 2011 19:39:06
Problema Portal3 Scor 100
Compilator cpp Status done
Runda .com 2011 Marime 1.47 kb
#include<fstream>
#include<iostream>
#include<queue>
using namespace std;

#define INF 2147483647
#define ll long long
#define pii pair<int,int>
#define mp make_pair
#define X first
#define Y second

ifstream f("portal3.in");
ofstream g("portal3.out");

ll T, N, M;
ll graf[10][10];
queue<ll>q;
pii nods[10];
ll sol[10];

ll abs(ll a)
{	if(a < 0) return -a;
	return a;
}

int dist(ll a, ll b)
{	return abs(nods[a].X - nods[b].X) + abs(nods[a].Y - nods[b].Y);
}

void bellman()
{	int i, j;
	ll c;
	
	q.push(1);
	sol[1] = 0;
	for(i = 2; i < 10; i++) sol[i] = INF;
	
	while(!q.empty())
	{	i = q.front(); q.pop();
	
		for(j = 1; j <= 8; j++)
		{	if(j == i) continue;
			c = graf[i][j];
			if(sol[j] > sol[i] + c)
				sol[j] = sol[i] + c, q.push(j);
		}
	}
}

int main()
{	ll i, j, x, y, x2, y2, c;

	nods[1] = mp(0, 0);
	
	f>>T;
	for(; T; T--)
	{	f>>N>>M;
		
		f>>x>>y>>x2>>y2>>c;
		nods[2] = mp(x, y);
		nods[3] = mp(x2, y2);
		
		graf[2][3] = graf[3][2] = c;
		
		f>>x>>y>>x2>>y2>>c;
		nods[4] = mp(x, y);
		nods[5] = mp(x2, y2);
		
		graf[4][5] = graf[5][4] = c;
		
		f>>x>>y>>x2>>y2>>c;
		nods[6] = mp(x, y);
		nods[7] = mp(x2, y2);
		
		graf[6][7] = graf[7][6] = c;
		
		nods[8] = mp(N, M);
		
		for(i = 1; i < 8; i++)
			for(j = (i == 2 || i == 4 || i == 6)?i + 2 : i + 1; j <= 8; j++)
				graf[i][j] = graf[j][i] = dist(i, j);
		
		bellman();
		g<<sol[8]<<'\n';
	}
	f.close();
	g.close();
	return 0;
}