Cod sursa(job #1438914)

Utilizator VladutZ94FMI Chichirau Vlad Vasile VladutZ94 Data 21 mai 2015 04:02:31
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#include <vector>
#include <string.h>
#define NMAX 30005


std::ifstream f("sate.in");
std::ofstream g("sate.out");
int d[NMAX], n, m, x, y, a, b, c;
std::vector <std::pair<int, int>>v[NMAX];

void BFS(int nod)
{
	for (int i = 0; i<v[nod].size(); i++)
		if (d[v[nod][i].first] == 0)
		{
			int r = v[nod][i].first;
			int w = v[nod][i].second;
			d[r] = d[nod] + w;
			BFS(r);
		}
}
int main()
{
	f >> n >> m >> x >> y;
	for (int i = 1; i <= m; i++)
	{
		f >> a >> b >> c;
		v[a].push_back(std::make_pair(b, c));
		v[b].push_back(std::make_pair(a, -c));
	}
	BFS(x);
	if (d[y]<0)
		g << -d[y];
	else
		g << d[y];

	return 0;
}