Cod sursa(job #3313097)

Utilizator biancalautaruBianca Lautaru biancalautaru Data 2 octombrie 2025 09:38:04
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <queue>
#include <vector>
#define DIM 30001
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, st, fn, d[DIM];
struct muchie {
	int vec, cost;
};
vector<muchie> v[DIM];
queue<int> q;
int main() {
	fin >> n >> m >> st >> fn;
	while (m--) {
		int x, y, c;
		fin >> x >> y >> c;
		v[x].push_back({y,c});
		v[y].push_back({x,c});
	}
	for (int i = 1; i <= n; i++)
		d[i] = -1;
	d[st] = 0;
	q.push(st);
	while (!q.empty()) {
		int nod = q.front();
		q.pop();
		for (int i = 0; i < v[nod].size(); i++) {
			int vec = v[nod][i].vec;
			int cost = v[nod][i].cost;
			if (d[vec] == -1) {
				if (vec > nod)
					d[vec] = d[nod] + cost;
				else
					d[vec] = d[nod] - cost;
				q.push(vec);
			}
		}
	}
	fout << d[fn];
	return 0;
}