Cod sursa(job #3300127)

Utilizator vladm98Munteanu Vlad vladm98 Data 12 iunie 2025 21:40:10
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#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;
}