Pagini recente » Cod sursa (job #519248) | Cod sursa (job #3123788) | Cod sursa (job #2251202) | Cod sursa (job #2650581) | Cod sursa (job #1394127)
#include <iostream>
#include <fstream>
#include<queue>
#include<algorithm>
#define valoare 99999999
using namespace std;
int main()
{
int *dist,k,i,x,y,n,m,b,c,d;
vector< pair <int,int> > *v;
queue<int> q;
ifstream f("sate.in");
ofstream g("sate.out");
f >> n >> m >> b >>c;
v = new vector< pair< int,int > >[n+1];
dist = new int [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<int,int > >::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;
}