Cod sursa(job #1194510)

Utilizator varga13VarGaz13 varga13 Data 3 iunie 2014 23:33:48
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <stdlib.h>
#define mx 30004
#define inf 2147483647
using namespace std;

int n,m,x,y, grad[mx];

struct muchie
{
    int cost;
    int dest;
}*Gr[mx];

muchie make_muchie(int c, int d)
{
    muchie aux;
    aux.cost=c;
    aux.dest=d;
    return aux;
}

vector< pair<int,int> > G[mx];


int best[mx];

ifstream f("sate.in");
ofstream g("sate.out");

void Initialisation()
{
    for(int i=0;i<mx;i++)
    {
        best[i]=0;
    }
}

void Read()
{
    f>>n>>m>>x>>y;
    Initialisation();
    for(int i=0;i<m;i++)
    {
        int a,b,d;
        f>>a>>b>>d;
        G[a].push_back( make_pair(b,d) );
        G[b].push_back( make_pair(a,-d) );
    }
}

void BFSit(int start, int fin)
{
    queue<int> Q;
    int curent=-1;
    for(Q.push(start);Q.size() && (curent!=fin);Q.pop())
    {
        curent=Q.front();

        for(int i=0;i<G[curent].size();i++)
        {
            if(!best[ G[curent][i].first ])
            {
                best[G[curent][i].first]=best[curent]+G[curent][i].second;
                Q.push(G[curent][i].first);
            }
        }

    }
}

void BFSrec(int curent)
{

    for(int i=0;i<G[curent].size();i++)
    {
        if(best[ G[curent][i].first ]==0 )
        {
            best[G[curent][i].first]=best[curent]+G[curent][i].second;
            if(G[curent][i].first!=y)
                BFSrec(G[curent][i].first);
        }
    }
}

int main()
{
    Read();
    BFSrec(x);
    g<<best[y];
    return 0;
}