Pagini recente » Cod sursa (job #988734) | Cod sursa (job #2710023) | Cod sursa (job #3168017) | Cod sursa (job #3187441) | Cod sursa (job #2966178)
#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] = a[x.first][y.first].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 <= n; 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;
}