Cod sursa(job #2650846)

Utilizator IRadu1529Radu Ionescu IRadu1529 Data 20 septembrie 2020 14:00:24
Problema Sate Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <fstream>
using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");

int n, m, x, y, gasit;

vector<int> ocupat(30001, 0);

struct nod
{
	int d, cost;
};

vector<vector<nod> > liste(30001, vector<nod>());

void bfs(int el, int cost)
{
	ocupat[el] = 1;

	if (el == y)
	{
		fout << cost;

		gasit = 1;

		return;
	}

	if(gasit == 0)
		for (int i = 0; i < liste[el].size(); i++)
			if (ocupat[liste[el][i].d] == 0 && gasit == 0)
				bfs(liste[el][i].d, cost + liste[el][i].cost);

	ocupat[el] = 0;
}

int main()
{
	fin >> n >> m >> x >> y;

	for (int i = 1; i <= m; i++)
	{
		int a, b, c;

		fin >> a >> b >> c;

		nod x, y;  x.d = b;  y.d = a;  x.cost = y.cost = c;

		if (a > b)
			x.cost *= -1;

		else
			y.cost *= -1;

		liste[a].push_back(x);

		liste[b].push_back(y);
	}

	bfs(x, 0);
}