Pagini recente » Cod sursa (job #2111107) | Cod sursa (job #534204) | Cod sursa (job #697737) | Cod sursa (job #1952401) | Cod sursa (job #1725197)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n, m, a, b, x, y, d, dist[30002], viz[30002];
vector<pair<int,int> > liste[30002];
int main()
{
f >> n >> m >> a >> b;
for( int i=1; i<=m; i++ )
{
f >> x >> y >> d;
liste[x].push_back(make_pair(y,d));
liste[y].push_back(make_pair(x,-d));
}
queue<int>q;
viz[a] = 1;
dist[a] = 0;
q.push(a);
while( !q.empty() )
{
x = q.front();
q.pop();
for( vector<pair<int,int> >::iterator it=liste[x].begin(); it!=liste[x].end(); it++ )
if( viz[it->first] == 0 )
{
viz[it->first] = 1;
dist[it->first] = dist[x] + it->second;
q.push(it->first);
}
}
g << dist[b];
return 0;
}