Cod sursa(job #2620346)

Utilizator Gliumarin negai Gliu Data 28 mai 2020 18:51:02
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <tuple>

using namespace std;

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


const int oo=(1<<31)-1;
int t,n,m,s,dist[100005],sal[100005];
bool viz[100005];

vector<tuple<int, int> >vb[100005];

void dijkstra(int nod){
	for(int i=1;i<=n;i++){
		dist=oo;
		viz[i]=0;
	}
	dist[nod]=0;
	priority_queue<tuple <int ,int >> pq[100005];
	pq.push({0,nod});
	while(!pq.empty()){
		int k=get<1>(pq.top());
		pq.pop();
		
		if(viz[k]) continue;
		
		for( auto& it : vb[k]){
			int b,c;
			tie(b,c) = it;
			if(dist[k]+ c <dist[b]){
				dist[b]=dist[k]+ c;
				pq.push({-dist[b], b});
			}
		}
		viz[k]=1;
		
	}
}

int main(){
	ios::sync_with_stdio(false);
	
in >>t;
while(t--){
	in >>n>>m>>s;
	for(int i=1;i<=n;i++)in >>sal[i]; 
	int a,b,c;
	for(int i=1;i<=m;i++){
		
		in>>a>>b>>c;
		vb[a].push_back({b,c});
		vb[b].push_back({a,c});
	}
	dijkstra(s);
	bool da=1;
	for(int i=1;i<=n;i++){
		if(sal[i] != dist[i]){
			out <<"NU\n";
			da=0;
			break; 
		}
	}
	if(da)out <<"DA\n";
	for(int i=1;i<=n;i++)vb[i].clear();

	
}

}