Cod sursa(job #2360500)

Utilizator urweakurweak urweak Data 1 martie 2019 21:17:26
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
typedef pair <int, int> PII;
#define NMax 50000
#define pb push_back

int N, M, P, S, drum[NMax], D[NMax];
const int oo = (1 << 30);
vector <PII> v[NMax];

void Citire(){
	for(int i = 1; i<=N; i++)
		in >> drum[i];
	for(int i = 1; i<=M; i++){
		int x, y, d;
		in >> x >> y >> d;
		v[x].pb({y,d});
	}
}
void Dijkstra(int k){
priority_queue<PII,vector<PII>,greater<PII>> Q;
Q.push({0,k});
D[k] = 0;
while(!Q.empty()){
	int len = Q.top().first, nod = Q.top().second;
	Q.pop();
	if(len!=D[nod])
		continue;
	for(auto it : v[nod])
		if(len + it.second < D[it.first]){
			D[it.first] = len + it.second;
			Q.push({D[it.first], it.first});
		}
}
}

int main(){
	 in.sync_with_stdio(false);
	in >> P;
	for(int i = 1; i<=P; i++){
		in >> N >> M >> S;
		Citire();
		for(int i = 1; i<=N ; i++)
			D[i] = oo;
		Dijkstra(S);
		int ok = 1;
		for(int i = 1; i<=N; i++){
			if(D[i] != drum[i])
					ok = 0;
		}

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