Cod sursa(job #3380608)

Utilizator carinaa_leetAmarie Carina carinaa_leet Data 2 decembrie 2026 07:36:10
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");

const int MAXN = 50005;

int D[MAXN];
bool ok[MAXN];

int main()
{
    int T;
    if (!(fin >> T))
        return 0;

    while (T--)
    {
        int n, m, s;
        fin >> n >> m >> s;

        for (int i = 1; i <= n; ++i)
        {
            fin >> D[i];
            ok[i] = false;
        }

        bool valid = true;

        // Conditia 1: distanta pana la sursa trebuie sa fie 0
        if (D[s] != 0)
        {
            valid = false;
        }
        ok[s] = true; // Sursa nu are nevoie de parinte

        for (int i = 1; i <= m; ++i)
        {
            int x, y, c;
            fin >> x >> y >> c;

            // Conditia 2: nicio muchie nu trebuie sa ofere o scurtatura
            if (D[x] > D[y] + c || D[y] > D[x] + c)
            {
                valid = false;
            }

            // Conditia 3: verificam daca muchia justifica distanta optima pentru x sau y
            if (D[x] == D[y] + c)
            {
                ok[x] = true;
            }
            if (D[y] == D[x] + c)
            {
                ok[y] = true;
            }
        }

        if (valid)
        {
            fout << "DA\n";
        }
        else
        {
            fout << "NU\n";
        }
    }

    return 0;
}