Pagini recente » Cod sursa (job #483443) | Cod sursa (job #685774) | Cod sursa (job #6422) | Cod sursa (job #1946035) | Cod sursa (job #2658993)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
int n, m, start, dx[100001], endd, dy[100001], ap[100001];
vector < pair < int, int > > G[100001];
queue < int > coada;
void bfs(int s, int d[]) {
bool vizitat[100001];
for (int i = 1; i <= n; i++) vizitat[i] = false;
vizitat[s] = true;
d[s] = 0;
coada.push(s);
while (!coada.empty()) {
int nod_curent = coada.front();
coada.pop();
for (auto u : G[nod_curent])
if (vizitat[u.first] == false) {
vizitat[u.first] = true;
coada.push(u.first);
d[u.first] = d[nod_curent] + u.second;
}
}
}
int main() {
ios::sync_with_stdio(false);
in.tie(NULL), out.tie(NULL);
in >> n >> m >> start >> endd;
for(int i = 1; i <= n; i++) dx[i] = -1, dy[i] = -1;
for (int i = 1; i <= m; i++) {
int x, y, dist;
in >> x >> y >> dist;
G[x].push_back(make_pair(y, dist));
G[y].push_back(make_pair(x, -dist));
}
bfs(start, dx);
out << dx[endd];
return 0;
}