Pagini recente » Cod sursa (job #1287155) | Cod sursa (job #95278) | Cod sursa (job #12342) | Cod sursa (job #2855672) | Cod sursa (job #3005178)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define pb push_back
#define N 30030
#define INF 1e18
#define FILE "sate"
struct nod {
int v;
int w;
};
int n, m, from, to;
vector<nod> g[N];
int64_t dist[N];
queue<int> q;
int main() {
freopen(FILE".in", "r", stdin);
freopen(FILE".out", "w", stdout);
cin >> n >> m >> from >> to;
for(int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, -w});
}
for(int i = 1; i <= n; i++) {
dist[i] = INF;
}
dist[from] = 0;
q.push(from);
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto nod : g[u]) {
if(dist[nod.v] == INF) {
dist[nod.v] = nod.w + dist[u];
q.push(nod.v);
}
}
}
cout << dist[to] << '\n';
}