Pagini recente » Cod sursa (job #1960254) | Cod sursa (job #440610) | Cod sursa (job #390122) | Cod sursa (job #2835017) | Cod sursa (job #3188612)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y;
vector<pair<int, int>> neighbours[30005];
queue<int> Q;
int distances[30005];
int main()
{
fin >> n >> m >> x >> y;
for (int i = 1; i <= m; i++) {
int a, b, length;
fin >> a >> b >> length;
neighbours[a].push_back(make_pair(b, length));
neighbours[b].push_back(make_pair(a, -length));
}
Q.push(x);
while (!Q.empty()) {
int current = Q.front();
for (int i = 0; i < neighbours[current].size(); i++) {
int currentNeighbour = neighbours[current][i].first;
if (distances[currentNeighbour] == 0) {
distances[currentNeighbour] = distances[current] + neighbours[current][i].second;
Q.push(currentNeighbour);
}
}
Q.pop();
}
fout << distances[y];
}