Cod sursa(job #2640062)

Utilizator denisfDenis Florin denisf Data 4 august 2020 20:37:48
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <fstream>
#include <vector>
#include <set>
#include <iterator>

using namespace std;

ifstream cin("distante.in");
ofstream cout("distante.out");

struct CostMuchie
{
    int Nod1,Nod2,val;
} cost[100001];

int d[50001],vf[50001];
int nrGrafuri,n,m,s,a,b,c,CostMinim;

void Drum(vector < pair <int, int> > M[], int NodCurent, int CostCurent)
{
    if (NodCurent == s && CostCurent < CostMinim)
    {
        CostMinim = CostCurent;
        return;
    }
    for (int i=0; i<M[NodCurent].size(); ++i)
    {
        int x=M[NodCurent][i].first;
        if (!vf[x])
        {
            vf[x]=1;
            Drum(M,x,CostCurent+M[NodCurent][i].second);
            vf[x]=0;
        }
    }
}

int main(void)
{
    cin >> nrGrafuri;
    for (int i=0; i<nrGrafuri; ++i)
    {
        cin >> n >> m >> s;
        for (int j=1; j<=n; ++j)
            cin >> d[j];
        vector < pair < int, int > > M[n+1];
        for (int j=0; j<m; ++j)
        {
            cin >> a >> b >> c;
            cost[j+1].Nod1 = a;
            cost[j+1].Nod2 = b;
            M[a].push_back({b,c});
            M[b].push_back({a,c});
        }
        bool OK = true;
        for (int j=1; j<=n && OK; ++j)
        {
            CostMinim = 100000000;
            if (j == s)
                CostMinim = 0;
            else
                Drum(M,j,0);
            if (CostMinim != d[j])
                OK = false;
        }
        if (OK)
            cout << "DA";
        else
            cout << "NU";
        cout << '\n';
    }
}