Pagini recente » Cod sursa (job #401956) | Cod sursa (job #1411666) | Cod sursa (job #2364877) | Cod sursa (job #150987) | Cod sursa (job #3140101)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int n, m, x, y;
vector <int> dist;
vector <vector <pair <int, int> > > graph;
void dfs(int node, int parent, int cost)
{
dist[node] = dist[parent] + cost;
for(auto neighbour : graph[node])
if(!dist[neighbour.first])
{
dfs(neighbour.first, node, neighbour.second);
}
}
int main()
{
ios_base :: sync_with_stdio(0);
cin.tie(0);
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
cin >> n >> m >> x >> y;
graph.resize(n + 1);
dist.resize(n + 1, 0);
for(int i = 1; i <= m; i ++)
{
int a, b, cost;
cin >> a >> b >> cost;
graph[a].pb({b, cost});
graph[b].pb({a, -cost});
}
dist[x] = 0;
dfs(x, x, 1);
cout << dist[y] - 1;
return 0;
}