Cod sursa(job #3378912)

Utilizator StefanVVulpoiu Stefan StefanV Data 28 noiembrie 2026 19:31:03
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>

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

int t, n, m, s, d[50005], ct;
bool are_parinte[50005];

int main()
{
    fin >>t;
    for (int i=1;i<=t;i++)
    {
        fin >>n>>m>>s;
        for (int j=1;j<=n;j++)
        {
            fin >> d[j];
            are_parinte[j] = false;
        }

        ct = 0;
        are_parinte[s] = true;

        if (d[s] != 0)
            ct = -1;

        for (int k=1;k<=m;k++)
        {
            int a, b, c;
            fin >>a>>b>>c;

            if ( d[b] > d[a] + c || d[a] > d[b] + c )
                ct = -1;

            if ( d[b] == d[a] + c )
                are_parinte[b] = true;

            if ( d[a] == d[b] + c )
                are_parinte[a] = true;
        }

        for (int j = 1; j <= n; j++)
        {
            if (!are_parinte[j])
                ct = -1;
        }

        if (ct==-1)
            fout <<"NU\n";
        else
            fout <<"DA\n";
    }
    return 0;
}