Cod sursa(job #2524918)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 16 ianuarie 2020 16:21:06
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int range[30001];
int n, m, x, y;

struct edge {
	int destination, distance;
};

vector<edge> g[30001];
queue<edge> q;

int main() {
	int a, b, dist;
	in >> n >> m >> x >> y;
	for (int i = 1; i <= m; i++) {
		in >> a >> b >> dist;
		g[a].push_back({ b, dist });
		g[b].push_back({ a, -dist });
	}
	q.push({ x, 0 });
	for (int i = 1; i <= n; i++)
		range[i] = -1;
	while (!q.empty()) {
		int node = q.front().destination;
		int length = q.front().distance;
		q.pop();
		range[node] = length;
		for (const auto& it : g[node]) {
			if (range[it.destination] == -1) 
				q.push({ it.destination, it.distance + length});
		}
	}
	out << range[y];
	return 0;
}