Pagini recente » Cod sursa (job #2466065) | Cod sursa (job #517173) | Cod sursa (job #3273082) | Borderou de evaluare (job #2743113) | Cod sursa (job #2966199)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 30001;
vector < pair<int, int> > a[NMAX];
vector <int> d(NMAX, -1);
void bfs(int s){
queue < pair<int, int> > q;
d[s] = 0;
q.push(make_pair(s, 0));
while(!q.empty()){
pair <int, int> x = q.front();
q.pop();
for(auto y : a[x.first]){
if(d[y.first] == -1){
d[y.first] = y.second + d[x.first];
q.push(y);
}
}
}
}
int main()
{
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
fin >> n >> m >> x >> y;
for(int i = 1; i <= m; i++){
int u, v, d;
fin >> u >> v >> d;
a[u].push_back(make_pair(v, d));
a[v].push_back(make_pair(u, -d));
}
bfs(x);
fout << d[y];
return 0;
}