Pagini recente » Borderou de evaluare (job #1036229) | Cod sursa (job #2002490) | Cod sursa (job #2289742) | Cod sursa (job #1836844) | Cod sursa (job #637631)
Cod sursa(job #637631)
#include <fstream>
#include <queue>
using namespace std;
struct portal
{
int x1, y1, x2, y2, cost;
} p[3];
int T, N, M;
int cost[8][8];
long long d[8];
inline int abs(int x) { return x > 0 ? x : -x; }
void bellmanford()
{
for (int i = 0; i < 8; ++i)
d[i] = 999999999999999LL;
d[0] = 0;
queue<int> Q;
for (Q.push(0); !Q.empty(); Q.pop())
{
int n = Q.front();
for (int i = 0; i < 8; ++i)
if (d[i] > d[n] + cost[n][i])
{
d[i] = d[n] + cost[n][i];
Q.push(i);
}
}
}
int main()
{
ifstream fin("portal3.in");
ofstream fout("portal3.out");
fin>>T;
while (T--)
{
fin>>N>>M;
for (int i = 0; i < 3; ++i)
{
fin>>p[i].x1>>p[i].y1>>p[i].x2>>p[i].y2>>p[i].cost;
cost[2*i+1][2*i+2] = cost[2*i+2][2*i+1] = p[i].cost;
}
cost[0][7] = cost[7][0] = N + M;
for (int i = 0; i < 3; ++i)
{
cost[0][2*i+1] = cost[2*i+1][0] = p[i].x1 + p[i].y1;
cost[0][2*i+2] = cost[2*i+1][0] = p[i].x2 + p[i].y2;
cost[7][2*i+1] = cost[2*i+1][7] = N - p[i].x1 + M - p[i].y1;
cost[7][2*i+2] = cost[2*i+2][7] = N - p[i].x2 + M - p[i].y2;
}
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
if (i == j) continue;
cost[2*i+1][2*j+1] = abs(p[i].x1 - p[j].x1) + abs(p[i].y1 - p[j].y1);
cost[2*i+1][2*j+2] = abs(p[i].x1 - p[j].x2) + abs(p[i].y1 - p[j].y2);
cost[2*i+2][2*j+1] = abs(p[i].x2 - p[j].x1) + abs(p[i].y2 - p[j].y1);
cost[2*i+2][2*j+2] = abs(p[i].x2 - p[j].x2) + abs(p[i].y2 - p[j].y2);
}
bellmanford();
fout<<d[7]<<"\n";
}
return 0;
}