Cod sursa(job #2400680)

Utilizator IordachescuAncaFMI Iordachescu Anca Mihaela IordachescuAnca Data 8 aprilie 2019 23:47:08
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include<fstream>
#include<vector>
#include<queue>
#define NMAX 50010
#define INF 1e9

using namespace std;

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

vector<pair<int,int>>G[NMAX];

int main()
{
	int t;
	fin >> t;

	for(int i = 0; i < t; i++)
	{
		int n, m, k;
		fin >> n >> m >> k;

		int ans[n+3]={0};
		for(int j = 1; j <= n; j++)
		{
			fin >> ans[j];
		}

		vector<int>dist(n+2,INF);
		dist[k] = 0;

		for(int j = 0; j < m; j++)
		{
			int x, y, cost;
			fin >> x >> y >> cost;
			G[x].push_back(make_pair(y, cost));
			G[y].push_back(make_pair(x, cost));
		}

		priority_queue<pair<int,int>>q;
		q.push(make_pair(0,k));
		bool viz[n+3]={0};

		while(!q.empty())
    	{
        	int x = q.top().second;
	        q.pop();
	
        	if(!viz[x])
        	{
            	viz[x] = true;
            	for(unsigned int k = 0; k < G[x].size(); k++)
            	{
                	if(dist[x] + G[x][k].second < dist[G[x][k].first])
                	{
                    dist[G[x][k].first] = dist[x] + G[x][k].second;
                    q.push(make_pair( -dist[G[x][k].first], G[x][k].first));
                	}
            	}
            }
	
        }

		bool ok = 1;
		for(int j = 1; j <= n; j++)
		{
			if(dist[j] != ans[j])
			{
				ok = 0;
				break;
			}
		}
		
		if(ok == 1)
		{
			fout << "DA\n";
		}
		else
		{
			fout << "NU\n";
		}

		for(int j = 1; j <= n; j++)
		{
			G[j].clear();
		}
	}

	fin.close();
	fout.close();
	return 0;
}