Cod sursa(job #433764)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 4 aprilie 2010 11:46:37
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include<fstream>
#include<queue>
#define inf "sate.in"
#define outf "sate.out"
#define NMax 30100
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int N,M,X,Y;
int D[NMax];
int viz[NMax];
queue<int> coada;

struct nod
{
int info;
int cost;
nod *next;
};

nod *LA[NMax];

inline void add(int a,int b,int c)
{
nod *t=new nod;
t->info=b;
t->cost=c;
t->next=LA[a];
LA[a]=t;
}

void read()
{
int a,b,c;
f>>N>>M>>X>>Y;
for(int i=1; i<=M; i++) f>>a>>b>>c , add(a,b,c) , add(b,a,c);
}

void solve()
{
nod *t;
int s;
coada.push(X); viz[X]=1;
while( !coada.empty() )
    {
    s = coada.front(); coada.pop();
    t = LA[s];
    while( t )
        {
        if( !viz[ t->info ] )
            {
            viz[ t->info ] = 1; coada.push( t->info );
            if( t->info > s ) D[ t->info ] = D[s] + t->cost ;
            else D[ t->info ] = D[s] - t->cost ;
            if( t->info == Y ) { g<<D[Y]; return; }
            }
        t=t->next;
        }
    }
}

int main()
{
read();
solve();
f.close();
g.close();
return 0;
}