Pagini recente » Istoria paginii runda/preoji2014_0_10/clasament | Cod sursa (job #1973590) | Cod sursa (job #2718798) | Cod sursa (job #2321614) | Cod sursa (job #2560680)
#pragma GCC optimize("O3")
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
int n, m, x, y;
vector<pair<int, int>> G[30005];
bool visited[30005];
queue<pair<int, int>> q;
int main() {
ios_base::sync_with_stdio(false);
in.tie(0);
out.tie(0);
in >> n >> m >> x >> y;
for (int i = 1; i <= m; i++) {
int a, b, d;
in >> a >> b >> d;
G[a].push_back({ b, d });
G[b].push_back({ a, -d });
}
q.push({ x, 0 });
visited[x] = 1;
while (!q.empty()) {
pair<int, int> current = q.front();
if (current.first == y) {
out << current.second;
return 0;
}
q.pop();
for (auto edge : G[current.first]) {
pair<int, int> newState = { edge.first, current.second + edge.second };
if (!visited[newState.first]) {
visited[newState.first] = 1;
q.push(newState);
}
}
}
}