Cod sursa(job #2596081)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 9 aprilie 2020 11:19:09
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int NMAX = 50005;

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

int m, n, s, x, a, b, cost;
int dist[NMAX];
int desired[NMAX];
bool viz[NMAX];

struct node {
	int v, cost;
};

struct muchie {
	int v, cost;
	muchie() {}
	muchie(int x, int cst)
	{
		v = x;
		cst = cost;
	}
	bool operator<(const muchie& other) const {
		return cost > other.cost;
	}
};

vector<node>G[NMAX];
priority_queue<muchie>pq;

void dijkstra(int nod)
{
	muchie temp(nod,0);
	viz[nod] = 1;
	pq.push(temp);
	while (!pq.empty())
	{
		temp = pq.top();
		pq.pop();
		for (int i = 0; i < G[temp.v].size(); i++)
		{
			int nnod = G[temp.v][i].v;
			int cost = G[temp.v][i].cost;
			if (!viz[nnod])
			{
				viz[nnod] = true;
				dist[nnod] = dist[temp.v] + cost;
				muchie temp1(nnod, temp.cost + cost);
				pq.push(temp1);
			}
		}
	}
}

int main()
{
	int t;
	fin >> t;
	while (t--)
	{
		fin >> n >> m >> s;
		while (!pq.empty())pq.pop();
		for (int i = 1; i <= n; i++) G[i].clear();
		for (int i = 1; i <= n; i++)
		{
			fin >> desired[i];
			dist[i] = 0;
			viz[i] = 0;
		}
		while (m--)
		{
			fin >> a >> b >> cost;
			node temp;
			temp.v = b;
			temp.cost = cost;
			G[a].push_back(temp);
			temp.v = a;
			G[b].push_back(temp);
		}
		dijkstra(s);
		bool ok = true;
		for(int i = 1 ; i <= n ; i++)
			if (dist[i] != desired[i])
			{
				ok = false;
				break;
			}
		if (ok)
			fout << "DA\n";
		else
			fout << "NU\n";
	}
	return 0;
}