Cod sursa(job #536164)

Utilizator feelshiftFeelshift feelshift Data 18 februarie 2011 12:35:56
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
// http://infoarena.ro/problema/distante
#include <fstream>
using namespace std;

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

int nrGraphs;
int nodes,edges,startNode;
int dist[50001];
bool canBeAchieved[50001];

void solve();

int main() {
	in >> nrGraphs;

	for(int step=1;step<=nrGraphs;step++) {
		in >> nodes >> edges >> startNode;

		solve();
	}

	in.close();
	out.close();

	return (0);
}

void solve() {
	int from,to,cost;
	bool ok = true;

	for(int i=1;i<=nodes;i++)
		in >> dist[i];

	if(dist[startNode])
		ok = false;

	if(ok)
		for(int i=1;i<=edges;i++) {
			in >> from >> to >> cost;

			// verificam daca distantele se pot imbunatati
			if(dist[from] + cost < dist[to] && dist[to] + cost < dist[from]) {
				ok = false;
				break;
			}

			// daca distantele sunt realizabile
			if(dist[from] + cost == dist[to])
				canBeAchieved[to] = true;

			if(dist[to] + cost == dist[from])
				canBeAchieved[from] = true;
		}
	else
		// datele tot trebuie citite, chiar daca nu se efectueaza operatiile
		for(int i=1;i<=edges;i++)
			in >> from >> to >> cost;

	if(ok)
		// verificam daca distantele sunt realizabile
		for(int currentNode=1;currentNode<=nodes;currentNode++)
			if(currentNode != startNode)
				if(!canBeAchieved[currentNode]) {
					ok = false;
					break;
				}

	memset(canBeAchieved,false,nodes+1);

	if(ok)
		out << "DA\n";
	else
		out << "NU\n";
}