Cod sursa(job #2917240)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 3 august 2022 21:59:12
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
/// Preset de infoarena
#include <fstream>
#include <vector>

using namespace std;

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

const int maxN = 50005;
int n, m, s, d[maxN];
struct mucie {
    int nod, cost;
};
vector <mucie> G[maxN];

bool check(int x)
{
    if(x == s)
        return (d[x] == 0);
    bool found = 0;
    for(auto nxt : G[x])
    {
        if(d[nxt.nod] + nxt.cost < d[x])
            return 0;
        if(d[nxt.nod] + nxt.cost == d[x])
            found = 1;
    }
    return found;
}

void solve()
{
    fin >> n >> m >> s;
    for(int i = 1; i <= n; i++)
        fin >> d[i];
    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c;
        G[x].push_back({y, c});
        G[y].push_back({x, c});
    }
    bool ok = 1;
    for(int i = 1; i <= n; i++)
        ok &= check(i);
    if(ok)
        fout << "DA\n";
    else
        fout << "NU\n";
    for(int i = 1; i <= n; i++)
        G[i].clear();
}

int main()
{
    int nr_teste;
    fin >> nr_teste;
    while(nr_teste--)
        solve();
    return 0;
}