Cod sursa(job #3219916)

Utilizator DobraVictorDobra Victor Ioan DobraVictor Data 1 aprilie 2024 19:47:48
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <utility>
#include <vector>
#include <queue>

typedef std::pair<int32_t, int32_t> pair;
typedef std::vector<pair> vector;
typedef std::priority_queue<pair, vector, std::greater<pair>> queue;

const int32_t MAX_N = 50000;
const int32_t MAX_COST = 1000000000;

int32_t res[MAX_N];
int32_t cost[MAX_N];
vector adj[MAX_N];
queue q;
int main() {
	std::ifstream fin("distante.in");
	std::ofstream fout("distante.out");

	int32_t t;
	fin >> t;

	while(t--) {
		int32_t n, m, s;
		fin >> n >> m >> s;
		--s;

		for(int32_t i = 0; i != n; ++i)
			fin >> res[i];
		for(int32_t i = 0; i != m; ++i) {
			int32_t x, y, c;
			fin >> x >> y >> c;
			--x; --y;
			adj[x].push_back({ y, c });
			adj[y].push_back({ x, c });
		}
		for(int32_t i = 0; i != n; ++i)
			cost[i] = MAX_COST;
		cost[s] = 0;
		q.push({ 0, s });

		while(!q.empty()) {
			pair val = q.top();
			q.pop();

			if(cost[val.second] < val.first)
				continue;
			
			for(pair next : adj[val.second]) {
				int32_t newCost = val.first + next.second;
				if(newCost >= cost[next.first])
					continue;
				
				cost[next.first] = newCost;
				q.push({ newCost, next.first });
			}
		}

		bool equal = true;
		for(int32_t i = 0; i != n && equal; ++i)
			equal = cost[i] == res[i];
		
		if(equal) {
			fout << "DA\n";
		} else {
			fout << "NU\n";
		}

		for(int32_t i = 0; i != n; ++i)
			adj[i].clear();
	}

	fin.close();
	fout.close();

	return 0;
}