Cod sursa(job #1101393)

Utilizator alexandru70Ungurianu Alexandru alexandru70 Data 8 februarie 2014 13:33:53
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <limits>

using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");

typedef vector<unsigned> vec_u;
typedef pair<unsigned,unsigned> pair_u;
typedef vector<pair_u> vec_pair_u;

const unsigned INF = numeric_limits<unsigned>::max();

class Graph {

public:
	Graph(size_t n) :
		nNodes(n)
	{
		adLs.resize(n);
	}

	void addEdge(unsigned from, unsigned to, unsigned cost) {
		adLs[from].push_back(pair_u(to,cost));
	}

	vec_u fromOnetoAll(unsigned s) {
		vec_u dist(nNodes,INF);
		set<pair_u> Q;

		Q.insert(pair_u(0,s));
		while(!Q.empty()) {
			pair_u top = *Q.begin();
			Q.erase(Q.begin());
			unsigned v1 = top.second;

			for(vec_pair_u::const_iterator it = adLs[v1].begin(); it != adLs[v1].end(); ++it) {
				unsigned v2 = it->first;
				unsigned cost = it->second;
				if(dist[v2] > dist[v1] + cost) {
					if(dist[v2] != INF)
						Q.erase(Q.find(pair_u(dist[v2],v2)));
					dist[v2] = dist[v1] + cost;
					Q.insert(pair_u(dist[v2],v2));
				}
			}
		}
		for(size_t i = 0; i < nNodes; ++i) {
			if(dist[i]==INF)dist[i] = 0;
			else dist[i]++;
		}
		dist[s]=0;
		return dist;
	}
	size_t getNNodes() {
		return nNodes;
	}
private:
	vector<vec_pair_u> adLs;
	size_t nNodes;
};

int main() {
	size_t t;
	in >> t;
	while(t--) {
		size_t n,m;
		unsigned s;
		in >> n >> m >> s;

		Graph g(n);

		vec_u bDists(n);
		for(size_t i = 0; i < n; ++i)
			in >> bDists[i];

		for(size_t i = 0; i < m; ++i) {
			unsigned a,b,c;
			in >> a >> b >> c;
			g.addEdge(a-1,b-1,c);
			g.addEdge(b-1,a-1,c);
		}
		
		if(bDists == g.fromOnetoAll(s-1))
			out << "DA\n";
		else
			out << "NU\n";
	}
}