Pagini recente » Cod sursa (job #1025588) | Cod sursa (job #2830433) | Cod sursa (job #1922724) | Cod sursa (job #2262813) | Cod sursa (job #1387835)
#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;
}