Cod sursa(job #2583331)

Utilizator mirceamaierean41Mircea Maierean mirceamaierean41 Data 18 martie 2020 02:03:35
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#pragma warning (disable:4996)
using namespace std;

typedef pair <int, long long> p;

const int NMAX = 30003;

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

vector <p> g[NMAX];
long long dist[NMAX], d;
int n, m, x, y, a, b;

bitset <NMAX> viz;

queue <int> q;

void solve()
{
	while (!q.empty())
	{
		x = q.front();
		viz[x] = 1;
		q.pop();
		if (x == b)
			return;
		vector <p> ::iterator it = g[x].begin(), f = g[x].end();
		for (; it != f; ++it)
		{
			if (viz[it->first] == 0)
			{
				dist[it->first] = dist[x] + it->second;
				q.push(it->first);
			}
		}
	}
}

int main()
{
	fin >> n >> m >> a >> b;
	for (int i = 1; i <= m; ++i)
	{
		fin >> x >> y >> d;
		g[x].push_back({ y, d });
		g[y].push_back({ x, -d });
	}
	dist[a] = 0;
	q.push(a);
	solve();
	fout << dist[b] << "\n";
	return 0;
}