Cod sursa(job #278401)

Utilizator recviemAlexandru Pana recviem Data 12 martie 2009 12:04:05
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <iostream>
#include <fstream>
using namespace std;

#define mMax 100001
#define nMax 50001
#define inf 0x3f3f3f3f

typedef struct{
	int x,y,c;
} muchie;

int nr, n, m, sursa,d1[nMax],d2[nMax];
string rez[2] = {"NU","DA"};
muchie G[mMax];
ifstream fin;

int bellmanford(){
	int cont = 1;
	while (cont){
		cont = 0;
		for (int i=0;i<m;i++){
			if (d2[G[i].y] > d2[G[i].x] + G[i].c)
				d2[G[i].y] = d2[G[i].x] + G[i].c, cont = 1;

			if (d2[G[i].y] < d1[G[i].y])
				return 0;
		}
	}
	for (int i=0;i<n;i++)
		if (d1[i] != d2[i])
			return 0;

	return 1;
}

void citire(){
	ifstream fin("distante.in");
	ofstream fout("distante.out");
	fin >> nr;
	// za main loop
	for (int i=0;i<nr;i++){
		// read za graph
		fin >> n >> m >> sursa;
		sursa -= 1;

		for (int j=0; j<n; j++){
			fin >> d1[j];
			d2[j] = inf;
		}
		d2[sursa] = 0;

		for (int j=0; j<m; j++){
			fin >> G[j].x >> G[j].y >> G[j].c;
			G[j].x -= 1, G[j].y -= 1;
			if ( G[j].x == sursa )
				d2[G[j].y] = G[j].c;
		}
		// compare za distances
		fout << rez[bellmanford()] << "\n";
	}
	fin.close(), fout.close();
}

int main(){
	citire();
	return 0;
}