Cod sursa(job #3376669)

Utilizator Alex112358Glavan Alexandru Alex112358 Data 18 noiembrie 2026 21:18:00
Problema Distante Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int T, N, M, S;

ifstream f("distante.in");
ofstream g("distante.out");

int main() {
	f >> T;
	
		while (T > 0){
		f >> N >> M >> S;

		vector<vector<pair<int, int>>> muchii;
		vector<int> costuri;
		priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> coada;
		vector<int> costuriDeVer;

		muchii.resize(N+1);
		costuri.assign(N + 1, 1001);
		costuriDeVer.assign(N + 1, 1001);

		for (int i = 1; i <= N; i++)
		{
			int cost;
			f >> cost;
			costuriDeVer[i] = cost;
		}

		for (int i = 1; i <= M; i++)
		{
			int a, b, c;
			f >> a >> b >> c;
			muchii[a].push_back(pair(b, c));
			muchii[b].push_back(pair(a, c));
		}

		for (int i = 1; i <= N; i++)
			if (i == S)
				costuri[i] = 0;
			else
				costuri[i] = 1001;


		
			coada.push(pair(0, S));
			while(!coada.empty())
			{
				int cost, nod;
				cost = coada.top().first;
				nod = coada.top().second;
				coada.pop();

				for (auto [vecin, costVecin] : muchii[nod])
				{
					if (costuri[vecin] > costuri[nod] + costVecin)
					{
						costuri[vecin] = costuri[nod] + costVecin;
						coada.push(pair(costuri[vecin], vecin));
					}
				}
			}
		
		bool ver = true;
		for (int i = 1; i <= N; i++)
			if (costuriDeVer[i] != costuri[i])
			{
				ver = false;
				break;
			}
		if (ver)
			g << "DA\n";
		else
			g << "NU\n";
		T--;
	}

	return 0;
}