Cod sursa(job #1857301)

Utilizator vladbatalanBatalan Vlad vladbatalan Data 26 ianuarie 2017 00:04:34
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>
#define MAXN 50010
using namespace std;

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

vector<pair<int, int> > v[MAXN];
set<pair<int, int> > h;
int n,m,s,T,x,y,w,ok;
int dist[MAXN];
int D[MAXN];

int main()
{
    int top, nod, cost;
    fin>>T;
    while(T--)
    {
        for(int i = 0; i<=n; i++)
            v[i].clear();
        fin>>n>>m>>s;
        for(int i=1; i<=n; i++)
            fin>>D[i];
        while(m--)
        {
            fin>>x>>y>>w;
            v[x].push_back(make_pair(y,w));
        }
        for(int i=1; i<=n; i++)
            dist[i] = MAXN;
        dist[s] = 0;
        h.insert(make_pair(0,s));
        while(!h.empty())
        {
            top = h.begin()->second;
            h.erase(h.begin());
            for(auto it:v[top])
            {
                nod = it.first;
                cost = it.second;
                if(dist[nod] > dist[top] + cost)
                {
                    if(dist[nod] != MAXN)
                        h.erase(h.find(make_pair(dist[nod], nod)));
                    dist[nod] = dist[top] + cost;
                    h.insert(make_pair(dist[nod], nod));
                }

            }
        }
        ok = 1;
        for(int i=1; i<=n; i++)
        {
            if(dist[i] == MAXN)dist[i] = 0;
            if(D[i] != dist[i])
            {
                ok=0;
                break;
            }
        }
        if(ok)
            fout<<"DA\n";
        else
            fout<<"NU\n";
    }
    return 0;
}