Cod sursa(job #638320)

Utilizator mihai995mihai995 mihai995 Data 20 noiembrie 2011 20:14:19
Problema Portal3 Scor 0
Compilator cpp Status done
Runda .com 2011 Marime 1.62 kb
#include <fstream>
using namespace std;

const int inf=1<<30;
struct portal{int x1,y1,x2,y2,c;} P1,P2,P3;
int n,m,rez;

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

inline int abs(int a)
{
    return a>0 ? a : -a;
}

inline int min(int a,int b)
{
    return a<b ? a : b;
}

inline int dist(int x,int y,int z,int t)
{
    return abs(z-x)+abs(t-y);
}

inline void read(portal& P)
{
    in>>P.x1>>P.y1>>P.x2>>P.y2>>P.c;
}

int update(int& x,int& y,portal P)
{
    int rez=P.c;
    if (dist(x,y,P.x1,P.y1)<dist(x,y,P.x2,P.y2))
    {
        rez+=dist(x,y,P.x1,P.y1);
        x=P.x2;
        y=P.y2;
    }
    else
    {
        rez+=dist(x,y,P.x2,P.y2);
        x=P.x1;
        y=P.y1;
    }
    return rez;
}

void move1(portal P)
{
    int x=0,y=0;
    rez=min(rez,update(x,y,P)+dist(x,y,n,m));
}

void move2(portal P1,portal P2)
{
    int x=0,y=0;
    rez=min(rez,update(x,y,P1)+update(x,y,P2)+dist(x,y,n,m));
}

void move3(portal P1,portal P2,portal P3)
{
    int x=0,y=0;
    rez=min(rez,update(x,y,P1)+update(x,y,P2)+update(x,y,P3)+dist(x,y,n,m));
}

int proc()
{
    in>>n>>m;
    read(P1);
    read(P2);
    read(P3);
    rez=dist(1,1,n,m);

    move1(P1);
    move1(P2);
    move1(P3);

    move2(P1,P2);
    move2(P1,P3);
    move2(P2,P1);
    move2(P2,P3);
    move2(P3,P1);
    move2(P3,P2);

    move3(P1,P2,P3);
    move3(P1,P3,P2);
    move3(P2,P1,P3);
    move3(P2,P3,P1);
    move3(P3,P1,P2);
    move3(P3,P2,P1);

    return rez;
}

int main()
{
    int t;
    in>>t;
    while (t--)
        out<<proc()<<"\n";
    return 0;
}