Cod sursa(job #1394134)

Utilizator AlexTAlexandru AlexT Data 20 martie 2015 00:55:42
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include<queue>
#include<algorithm>
#define valoare 99999
using namespace std;
int main()
{
long *dist,k,i,x,y,n,m,b,c,d;
vector< pair <long,long> > *v;
queue<long> q;
ifstream f("sate.in");
ofstream g("sate.out");
f >> n >> m >> b >>c;
v = new vector< pair< long,long > >[n+1];
dist = new long [n+1];
for(i=0;i<m;i++)
    {
    f >> x >> y >> d;
    v[x].push_back(make_pair(y,d));
    v[y].push_back(make_pair(x,-d));
    }
for(i=1;i<=n;i++)
    {
    dist[i]=valoare;
    }
    dist[b]=0;
q.push(b);
while(!q.empty())
{
k=q.front();
q.pop();
for(vector< pair<long,long > >::iterator i=v[k].begin();i!=v[k].end();i++)
    {
    if(dist[i->first]==valoare)
        {
        dist[i->first]=dist[k]+i->second;
        if(i->first==c)
            {
            g<<dist[i->first];
            return 0;
            }
        q.push(i->first);
        }
    }
}
f.close();
g.close();
return 0;
}