Cod sursa(job #976295)

Utilizator SwissRhinoSwissRhino SwissRhino Data 22 iulie 2013 23:04:20
Problema Distante Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int max_n = 50005;

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

queue<int> bfs;
int n, m, s;
vector<pair<int,int> > vertex[max_n];
int best[max_n];

bool viz[max_n];

int main() {
	int t;
	in >> t;
	while (t--) {
    	in >> n >> m >> s;
		for (int i = 1; i <= n; ++i)
			vertex[i].clear();
		for (int i = 1; i <= n; ++i)
			in >> best[i];
		for (int i = 1; i <= m; ++i) {
			int a, b, c;
			in >> a >> b >> c;
			vertex[a].push_back(make_pair(b, c));
			vertex[b].push_back(make_pair(a, c));
		}
		bool ok = true;
		if (best[s] != 0)
			ok = false;
		best[s] = 0;
		while (bfs.size())
			bfs.pop();
		bfs.push(s);
		viz[s] = true;
		while (bfs.size()) {
			int nod = bfs.front();
 			bfs.pop();
			for (int i = 0; i < int(vertex[nod].size()); ++i) {
				if (viz[vertex[nod][i].first])
					continue;
 				if (best[vertex[nod][i].first] == best[nod] + vertex[nod][i].second) {
					viz[vertex[nod][i].first] = true;
					bfs.push(vertex[nod][i].first);
				}
			}
		}
		for (int i = 1; i <= n; ++i)
			if (viz[i] == 0)
 				ok = false;
		for (int i = 1; i <= n; ++i)
			for (int j = 0; j < int(vertex[i].size()); ++j)
				if (best[vertex[i][j].first] > best[i] + vertex[i][j].second)
					ok = false;
 		if (ok)
			out << "DA\n";
		else
			out << "NU\n";
	}
	return 0;
}