Pagini recente » Cod sursa (job #1012339) | Cod sursa (job #2381259) | Cod sursa (job #1390471) | Cod sursa (job #1474265) | Cod sursa (job #637978)
Cod sursa(job #637978)
#include <fstream>
#include <algorithm>
#include <utility>
using namespace std;
#define x first
#define y second
const long long INF = 1LL << 60;
int T, N, M;
pair<int, int> C1[8], C2[8];
long long Cost[8], result;
inline int iabs(int x)
{
return (x < 0 ? -x : x);
}
inline long long dist(pair<int, int> i1, pair<int, int> i2)
{
return 1LL * iabs(i1.x - i2.x) + 1LL * iabs(i1.y - i2.y);
}
int main()
{
ifstream fin("portal3.in");
ofstream fout("portal3.out");
fin >> T;
while (T--)
{
fin >> N >> M;
fin >> C1[1].x >> C1[1].y >> C2[1].x >> C2[1].y >> Cost[1];
fin >> C1[2].x >> C1[2].y >> C2[2].x >> C2[2].y >> Cost[2];
fin >> C1[3].x >> C1[3].y >> C2[3].x >> C2[3].y >> Cost[3];
C1[4] = C2[1], C2[4] = C1[1], Cost[4] = Cost[1];
C1[5] = C2[2], C2[5] = C1[2], Cost[5] = Cost[2];
C1[6] = C2[3], C2[6] = C1[3], Cost[6] = Cost[3];
C1[7] = make_pair(N, M);
result = INF;
for (int i = 0; i <= 6; ++i)
for (int j = 0; j <= 6; ++j)
for (int k = 0; k <= 6; ++k)
result = min(result, dist(C1[0], C1[i]) + dist(C2[i], C1[j]) + dist(C2[j], C1[k]) + dist(C2[k], C1[7]) + Cost[i] + Cost[j] + Cost[k]);
fout << result << '\n';
}
fin.close();
fout.close();
}