Cod sursa(job #3237137)

Utilizator DomnulMilandruMilandru Nicon-David DomnulMilandru Data 5 iulie 2024 16:34:18
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb


#include <fstream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
ifstream cin("sate.in");
ofstream cout("sate.out");
int n, m;
int x, y;
int a, b, c;
struct muchie {
	int vecin;
	int cost;
};
vector<vector<muchie>> A;
vector<bool> fr;
int main()
{
	cin >> n >> m;
	cin >> x >> y;
	A.resize(n + 1);
	fr.resize(n + 1);
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		if (a > b)
			swap(a, b);
		A[a].push_back({ b,c });
		A[b].push_back({ a,-c });
	}
	queue<pair<int,int>> Q;
	Q.push({ x,0 });
	fr[x] = 1;
	while (!Q.empty())
	{
		int cap = Q.front().first;
		int cost = Q.front().second;
		Q.pop();
		if (cap == y)
		{
			cout << cost;
			exit(0);
		}
		for (int i = 0; i < A[cap].size(); i++)
		{
			int vecin = A[cap][i].vecin;
			int new_cost = A[cap][i].cost + cost;
			if (!fr[vecin])
			{
				fr[vecin] = 1;
				Q.push({ vecin,new_cost });
			}
		}
	}
	return 0;
}