Cod sursa(job #2518069)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 4 ianuarie 2020 21:21:51
Problema Distante Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <set>

using namespace std;

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

struct nod{
	int a, v;
	bool operator<(const nod &rhs)const{
		if(v != rhs.v){
			return v < rhs.v;
		}else{
			return a < rhs.a;
		}
	}
};

vector<int> gra[50041];

int t;
int n, m, s;

int ey[50041];

int va[50041];
bool vi[50041];

struct muc{
	int a, b;
	int v;
	int ott(int x){
		if(x == a){
			return b;
		}else{
			return a;
		}
	}
};
muc wu[100041];

void read(){
	fin >> n >> m >> s;
	for(int i = 1; i <= n; ++i){
		fin >> ey[i];
		va[i] = vi[i] = 0;
	}
	for(int i = 0; i < m; ++i){
		muc &x = wu[i];
		fin >> x.a >> x.b >> x.v;
		gra[x.a].push_back(i);
		gra[x.b].push_back(i);
	}
}

set<nod> se;

void addme(nod x){
	if(!vi[x.a] && (va[x.a] == 0 || x.v < va[x.a])){
		if(va[x.a] != 0){
			se.erase({x.a, va[x.a]});
		}
		se.insert(x);
		va[x.a] = x.v;
	}
}

bool idkjstra(){
	se.insert({s, 0});
	while(!se.empty()){
		nod x = *se.begin();
		se.erase(se.begin());
		vi[x.a] = 1;
		
		if(va[x.a] != ey[x.a]){
			return false;
		}
		
		vector<int> &v = gra[x.a];
		while(!v.empty()){
			int i = v.back();
			v.pop_back();
			muc y = wu[i];
			addme({y.ott(x.a), x.v+y.v});
		}
	}
	return true;
}

int main(){
	fin >> t;
	for(int i = 0; i < t; ++i){
		read();
		bool ok = idkjstra();
		if(ok){
			fout << "DA";
		}else{
			fout << "NU";
		}
		fout << "\n";
	}
	return 0;
}