Pagini recente » Cod sursa (job #1001657) | Cod sursa (job #2097619) | Cod sursa (job #1570961) | Cod sursa (job #227499) | Cod sursa (job #637090)
Cod sursa(job #637090)
#include <fstream>
using namespace std;
typedef struct portal
{
int x1,y1,x2,y2;
int c;
int m;
int r;
int f; // parinte
}
portal;
portal p[6];
//inline int calc_dist(int j ,int k)
//{
// int temp;
// if (p[j].x1 > p[k].x2)
// temp = p[j].x1 - p[k].x2;
// else
// temp = p[k].x2 - p[j].x1;
// if (p[j].y1 > p[k].y2)
// temp+= p[j].y1 - p[k].y2;
// else
// temp+= p[k].y2 - p[j].y1;
// return temp;
//}
//void update(int k)
//{
// int j,temp;
// for (j=0;j<3;j++)
// if (p[j].f==k)//il ai pe parinte pe k;
// {
// temp = calc_dist(j,k);
// //temp e distanta de la portalul k la portalul i;
// if ((temp + p[k].m + p[k].c)<=p[j].m) //daca drumu prin portalul k e mai mic decat drum drept
// {
// p[j].m = temp + p[k].m + p[k].c;
// update(j); // update la toti ce il au ca parinte pe i;
// }
// }
//}
int main()
{
ifstream f ("portal3.in");
ofstream g ("portal3.out");
int i,j,k,t,temp;
int n,m,min;
f>>t;
for (i=0;i<t;i++)
{
f>>n>>m;
for (j=0;j<3;j++)
{
f>>p[j].x1>>p[j].y1>>p[j].x2>>p[j].y2>>p[j].c;
p[j*2+1] = p[j];
p[j*2+1].x1 = p[j].x2;
p[j*2+1].y1 = p[j].y2;
p[j*2+1].x2 = p[j].x1;
p[j*2+1].y2 = p[j].y1;
p[j].m = p[j].x1+p[j].y1;
p[j*2+1].m = p[j*2+1].x1+p[j*2+1].y1;
p[j].r = n-p[j].x2 + m-p[j].y2 + p[j].c;
p[j*2+1].m = n- p[j*2+1].x2 + m-p[j*2+1].y2 +p[j*2+1].c;
p[j].f = j;
p[j*2+1].f =j*2+1;
}
for (j=0;j<6;j++)
{
for (k=0;k<6;k++)
{
if (k!=j)
{
if (p[j].x1 > p[k].x2)
temp = p[j].x1 - p[k].x2;
else
temp = p[k].x2 - p[j].x1;
if (p[j].y1 > p[k].y2)
temp+= p[j].y1 - p[k].y2;
else
temp+= p[k].y2 - p[j].y1;
//temp e distanta de la portalul k la portalul i;
if ((temp + p[k].m + p[k].c)<p[j].m) //daca drumu prin portalul k e mai mic decat drum drept
{
p[j].m=temp+p[k].m+p[k].c;
p[j].f=k; // toti cei ce il au pe f ca parinte trebuie sa faca update;
j=0;//reset the index
k=0;//reset the index
}
}
}
}
min=n+m;
for (j=0;j<6;j++)
if (p[j].m + p[j].r < min)
min = p[j].m + p[j].r;
g<<min<<"\n";
}
f.close();
g.close();
return 0;
}