Pagini recente » Cod sursa (job #993030) | Cod sursa (job #370449) | Cod sursa (job #2327064) | Cod sursa (job #1699830) | Cod sursa (job #637104)
Cod sursa(job #637104)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define inf 1000000100
#define pii pair<int, int>
#define LL long long
#define x first
#define y second
int T, N, M;
int C[4], B[4];
pair<pii, pii> P[4];
vector<pair< pair<pii, pii>, int> > V;
int main() {
FILE *f1=fopen("portal3.in", "r"), *f2=fopen("portal3.out", "w");
int i, j, p, q;
p = 1;
for(i=1; i<=3; i++) {
p = 3 * p;
}
fscanf(f1, "%d\n", &T);
while(T--) {
fscanf(f1, "%d %d\n", &N, &M);
//(0,0) -> (N,M)
for(i=1; i<=3; i++) {
fscanf(f1, "%d %d %d %d %d\n", &P[i].x.x, &P[i].x.y, &P[i].y.x, &P[i].y.y, &C[i]);
}
LL sol = inf;
for(int ki=0; ki<p; ki++) {
q = ki;
for(j=1; j<=3; j++) {
B[j] = (q % 3);
q = q / 3;
}
V.clear();
for(j=1; j<=3; j++) {
if(B[j] == 1) {
V.push_back( make_pair( make_pair(P[j].x, P[j].y), C[j]) );
}
else if(B[j] == 2) {
V.push_back( make_pair( make_pair(P[j].y, P[j].x), C[j]) );
}
}
if(V.size() == 0) {
sol = min(sol, (LL)N + M);
continue;
}
do {
LL aux = 0;
for(j=0; j<V.size(); j++) {
if(j == 0 && j == V.size()-1) {
aux += V[j].first.x.x + V[j].first.x.y + V[j].second;
aux += abs(N - V[j].first.y.x) + abs(M - V[j].first.y.y);
}
else if(j == 0) {
aux += (V[j].first.x.x + V[j].first.x.y + V[j].second);
}
else {
//(V[j-1].first.y.x, V[j-1].first.y.y) -> (V[j].first.x.x, V[j].first.x.y)
aux += (abs(V[j].first.x.x - V[j-1].first.y.x) + abs(V[j].first.x.y - V[j-1].first.y.y));
aux += V[j].second;
if(j == V.size()-1) {
aux += (abs(N - V[j].first.y.x) + abs(M - V[j].first.y.y));
}
}
}
sol = min(sol, aux);
} while(next_permutation(V.begin(), V.end()));
}
fprintf(f2, "%lld\n", sol);
}
fclose(f1); fclose(f2);
return 0;
}