Cod sursa(job #2640057)

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

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,CostMinim;

int Cost(int x, int y)
{
    for (int i=1; i<=m; ++i)
        if ((cost[i].Nod1 == x && cost[i].Nod2 == y) || ((cost[i].Nod1 == y && cost[i].Nod2 == x)))
            return cost[i].val;
}

void Drum(vector <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];
        if (!vf[x])
        {
            vf[x]=1;
            Drum(M,x,CostCurent+Cost(NodCurent,x));
            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 <int> M[n+1];
        for (int j=0; j<m; ++j)
        {
            cin >> a >> b >> cost[j+1].val;
            cost[j+1].Nod1 = a;
            cost[j+1].Nod2 = b;
            M[a].push_back(b);
            M[b].push_back(a);
        }
        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';
    }
}