Cod sursa(job #389020)

Utilizator cristiprgPrigoana Cristian cristiprg Data 31 ianuarie 2010 17:53:53
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <cstdio>
#define DIM 50005

struct nod
{
    int x, cost;
    nod *next;
};

nod *G[DIM];
int d[DIM], t, n, m, S;
const int INF = (1<<30);

void add(int i, int j, int c)
{
    nod *p = new nod;
    p -> x = j;
    p -> cost = c;
    p -> next = G[i];
    G[i] = p;
}

int min(int i)
{
    int MIN = INF;
    for (nod *p = G[i]; p; p = p -> next)
        if (MIN > d[p->x] + p -> cost )
            MIN = d[p->x] + p -> cost;

    return MIN;
}

bool solve()
{
    if (d[S] != 0)
        return false;

    for (int i = 1, M; i <= n; ++i)
        if (i != S)
        {
            M = min(i);
            if (M != d[i])
            {

                return false;
            }
        }
    return true;
}

void curata()
{
    for (int i = 1; i <= n; ++i)
        G[i] = NULL;
}

int main()
{
    FILE *f = fopen("distante.in", "r"), *f2 = fopen("distante.out", "w");

    fscanf(f,"%d", &t);
    for (;t;--t)
    {
        fscanf(f, "%d%d%d", &n, &m, &S);
        for (int i = 1; i <= n; ++i)
            fscanf(f, "%d", &d[i]);

        for (int i = 1, x, y, c; i <= m; ++i)
            fscanf(f, "%d%d%d", &x, &y, &c), add(x, y, c), add(y, x, c);

        fprintf(f2, "%s\n", solve()==true?"DA":"NU");
        curata();
    }
    fclose(f);
    fclose(f2);

    return 0;
}