Cod sursa(job #523549)

Utilizator feelshiftFeelshift feelshift Data 18 ianuarie 2011 15:51:06
Problema Distante Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 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;
	memset(canBeAchieved,false,sizeof(bool));
	
	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;
				}
	
	if(ok)
		out << "DA\n";
	else
		out << "NU\n";
}