Cod sursa(job #1586671)

Utilizator SmarandaMaria Pandele Smaranda Data 1 februarie 2016 16:18:02
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int N = 50003;

int n, m, s, d [N], num;
bool ok, used [N];
vector <int> graph [N];

void read () {
    int i, x, y, c;

    scanf ("%d%d%d", &n, &m, &s);
    for (i = 1; i <= n; i ++)
        scanf ("%d", &d [i]);
    if (d [s])
        ok = 0;
    for (i = 1; i <= m && ok; i ++) {
        scanf ("%d%d%d", &x, &y, &c);
        if (d [x] < d [y]) {
            if (d [x] + c == d [y])
                graph [x].push_back (y);
            else
                if (d [x] + c < d [y])
                    ok = 0;
        }
        else {
            if (d [y] + c == d [y])
                graph [y].push_back (x);
            else
                if (d [y] + c < d [x])
                    ok = 0;
        }
    }
}

void dfs (int x) {
    vector <int> :: iterator it;

    used [x] = 1;
    num ++;
    for (it = graph [x].begin (); it != graph [x].end (); ++ it)
        if (!used [*it])
            dfs (*it);
}

void solve () {
    int i;

    num = 0;
    dfs (s);
    if (num != n)
        ok = 0;
    memset (used, 0, sizeof (used));
    for (i = 1; i <= n; i ++)
        if (!graph [i].empty ())
            graph [i].clear ();
}

int main () {
    int T, t;

    freopen ("distante.in", "r", stdin);
    freopen ("distante.out", "w", stdout);

    scanf ("%d", &T);
    for (t = 1; t <= T; t ++) {
        ok = 1;
        read ();
        if (ok == 1)
            solve ();
        if (ok == 1)
            printf ("DA\n");
        else printf ("NU\n");
    }
    return 0;
}