Cod sursa(job #1560657)

Utilizator starlingIon Popa starling Data 2 ianuarie 2016 23:13:37
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
#define MAX 50008
#define INF 9999999999
vector <pair<int,int> > v[MAX];
long long D[MAX],d[MAX];
int t,i,j,k,n,m,s,a,b,c;
queue <int> Q;
void bfs(int nod)
{
    for(int i=1;i<=n;i++)
        D[i]=INF;
    D[nod]=0;
    Q.push(nod);
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        for(vector <pair<int,int> >::iterator it=v[x].begin();it!=v[x].end();it++)
            if(D[it->first]>D[x]+it->second)
            {
                Q.push(it->first);
                D[it->first]=D[x]+it->second;
            }
    }

}
int main()
{

    in>>t;
    while(t--)
    {  in>>n>>m>>s;
       for(int i=1;i<=n;i++)
        in>>d[i];
       for(int i=1;i<=m;i++)
       {
           in>>a>>b>>c;
           v[a].push_back({b,c});
           v[b].push_back({a,c});
       }
       bfs(s);
       int adev=1;
       for(int i=1;i<=n;i++)
        if(D[i]!=d[i])adev=0;
        if(adev==0)out<<"NU";
        else out<<"DA";
        out<<'\n';

    }
    return 0;
}