Cod sursa(job #721149)

Utilizator mottyMatei-Dan Epure motty Data 23 martie 2012 13:00:01
Problema Distante Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <set>

using namespace std;

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

const int INF = 0x3f3f3f3f;
const int N = 10001;

int n, source;

int dRead[N];
int dCur[N];

vector < pair<int,int> > g[N];
set < pair<int,int> > s;

void readCase() {
	int m;
	in >> n >> m >> source;

	for (int i = 1; i <= n; ++i) {
		in >> dRead[i];
		g[i].clear();
	}

	while (m--) {
		int a, b, c;
		in >> a >> b >> c;

		g[a].push_back(make_pair(b, c));
		g[b].push_back(make_pair(a, c));
	}
}

void check(int node) {
	for (int i = 0; i < (int)g[node].size(); ++i) {
		int next = g[node][i].first;

		if (dCur[node] + g[node][i].second < dCur[next]) {
			dCur[next] = dCur[node] + g[node][i].second;
			s.insert(make_pair(dCur[next], next));
		}
	}
}

string solveCase() {
	memset(dCur, INF, sizeof(dCur));
	dCur[source] = 0;
	s.insert(make_pair(0, source));

	while (!s.empty()) {
		pair <int,int> bighin = *s.begin();
		int node = bighin.second;
		s.erase(s.begin());

		if (dCur[node] != dRead[node])
			return "NU";

		check(node);
	}

	return "DA";
}

int main() {
	int t;
	in >> t;

	while (t--) {
		readCase();
		out << solveCase() << "\n";
	}

	return 0;
}