Cod sursa(job #2596114)

Utilizator mircearoataMircea Roata Palade mircearoata Data 9 aprilie 2020 11:54:08
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int m, n, s, x;
int dist[50005];
int targetDist[50005];

struct edge {
	int to, cost;
};

struct state {
	int nod, cost;
};

bool operator<(const state& a, const state& b) {
	return !(a.cost <= b.cost);
}

vector<edge>G[50005];

int main() {
	int t;
	in >> t;
	while (t--)	{
		in >> n >> m >> s;
		for (int i = 1; i <= n; i++) {
			G[i].clear();
		}
		for (int i = 1; i <= n; i++) {
			in >> targetDist[i];
		}
		for(int i = 1; i <= m; i++) {
			int x, y, cost;
			in >> x >> y >> cost;
			G[x].push_back({ y, cost });
			G[y].push_back({ x, cost });
		}
		for (int i = 1; i <= n; i++)
			dist[i] = (1 << 30);
		dist[s] = 0;

		priority_queue<state> q;
		q.push({ s, 0 });
		while (!q.empty()) {
			state current = q.top();
			q.pop();
			for (auto x : G[current.nod]) {
				if (current.cost + x.cost < dist[x.to]) {
					dist[x.to] = current.cost + x.cost;
					q.push({ x.to, current.cost + x.cost });
				}
			}
		}
		for (int i = 1; i <= n; i++)
			if (dist[i] == (1 << 30))
				dist[i] = 0;

		bool ok = true;
		for (int i = 1; i <= n; i++)
			if (dist[i] != targetDist[i]) {
				ok = false;
				break;
			}
		out << (ok ? "DA" : "NU") << '\n';
	}
	return 0;
}