Cod sursa(job #1387835)

Utilizator cautionPopescu Teodor caution Data 14 martie 2015 19:00:44
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <algorithm>
#include <queue>
#define SPECIAL_VALUE 123456789
using namespace std;
int main()
{
    ifstream in("sate.in");
    ofstream out("sate.out");
    long n, m, x, y, a, b, c, aux;
    vector<pair<long, long> > *graf;
    queue<long> que;
    long *dist;
    in>>n>>m>>x>>y;
    graf = new vector<pair<long, long> >[n+1];
    dist = new long[n+1];
    for(long i=0; i<m; ++i)
    {
        in>>a>>b>>c;
        graf[a].push_back(make_pair(b, c));
        graf[b].push_back(make_pair(a, -c));
    }
    for(long i=1; i<=n; ++i)
        dist[i]=SPECIAL_VALUE;
    dist[x]=0;
    que.push(x);
    while(!que.empty())
    {
        aux=que.front();
        que.pop();
        for(vector<pair<long, long> >::iterator it=graf[aux].begin(), ed=graf[aux].end(); it!=ed; ++it)
        {
            if(dist[it->first]==SPECIAL_VALUE)
            {
                dist[it->first]=dist[aux]+it->second;
                if(it->first==y)
                {
                    out<<dist[it->first]<<endl;
                    return 0;
                }
                que.push(it->first);
            }
        }
    }
    in.close(); out.close();
    return 0;
}