Cod sursa(job #2187974)

Utilizator petrea1551Petrea Calin petrea1551 Data 26 martie 2018 20:58:54
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>
using namespace std;
 
int dist[50100], a[50010], viz[50010];
priority_queue<pair<int, int>, vector<pair <int, int> >, greater<pair <int, int> > > q;
 
bool cmp(int n)
{
	for (int i = 1; i <= n; i++)
		if (a[i] != dist[i])
			return 0;
	return 1;
}
 
int main()
{
    ifstream cin("distante.in");
    ofstream cout("distante.out");
    int n, m, t, s;
    cin >> t;
    for (int h = 1; h <= t; h++)
    {
		vector<pair<int, int> > v[50001];
    	cin >> n >> m >> s;
    	for (int i = 1; i <= n; i++)
    	{
    		cin >> a[i];
    		viz[i] = 0;
    	}
    	for (int i = 1; i <= m; i++)
    	{
    	    int x, y, z;
    	    cin >> x >> y >> z;
    	    v[x].push_back({y, z});
    	    v[y].push_back({x, z});
    	    
    	}
    	for (int i = 1; i <= n; i++)
    	    dist[i] = 443446554;
    	dist[s] = 0;
    	q.push({0, s});
    	while (!q.empty())
    	{
    	    int c = q.top().second;
    	    int d = q.top().first;
    	    q.pop();
    	    if (viz[c])
    	    	continue;
    		viz[c] = 1;
       		int p = v[c].size();
        	for (int i = 0; i < p; i++)
       		{
        		int destination = v[c][i].first;
        		int weight = v[c][i].second;
        		if (dist[c] + v[c][i].second < dist[destination])
        		{
        			dist[destination] = dist[c] + weight;
        			q.push({dist[destination], destination});
				}
			}
    	}
    	if (!cmp(n))
    		cout << "NU\n";
    	else
    		cout << "DA\n";
	}
}