Pagini recente » Cod sursa (job #581581) | Clasament Junior Challenge 2012 | Cod sursa (job #3240905) | Cod sursa (job #3187266) | Cod sursa (job #3300127)
#include <bits/stdc++.h>
using namespace std;
vector <pair <int, int>> graph[100005];
int visited[100005];
void dfs(int node, int currentCost, int &answer, int y) {
if (node == y) {
answer = currentCost;
}
visited[node] = 1;
for (auto x : graph[node]) {
if (visited[x.first]) continue;
dfs(x.first, currentCost + x.second, answer, y);
}
}
int main()
{
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
int n, m, x, y;
cin >> n >> m >> x >> y;
for (int i = 1; i <= m; ++i) {
int x, y, c;
cin >> x >> y >> c;
graph[x].push_back({y, c});
graph[y].push_back({x, -c});
}
int answer = -1;
dfs(x, 0, answer, y);
cout << answer;
return 0;
}