Cod sursa(job #326200)

Utilizator DraStiKDragos Oprica DraStiK Data 24 iunie 2009 12:02:13
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <stdio.h>
#define DIM 50005

struct nod {int x,c;
            nod *urm;} *lst[DIM];
int dis[DIM],viz[DIM];
int t,n,m,p,s;

void clean ()
{
    nod *p,*q;
    int i;
    p=0;
    for (i=1; i<=n; ++i)
    {
        for (p=lst[i]; p; p=q)
        {
            q=p->urm;
            delete p;
        }
        lst[i]=NULL;
        viz[i]=0;
    }
}

void add (int srs,int dst, int cst)
{
    nod *p=new nod;
    p->x=dst;
    p->c=cst;
    p->urm=lst[srs];
    lst[srs]=p;
}

void read ()
{
    int i,x,y,ct;
    scanf ("%d%d%d",&n,&m,&s);
    for (i=1; i<=n; ++i)
        scanf ("%d",&dis[i]);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d%d%d",&x,&y,&ct);
        add (x,y,ct);
        add (y,x,ct);
    }
}

void distante (int srs)
{
    nod *p;
    viz[srs]=1;
    for (p=lst[srs]; p; p=p->urm)
        if (dis[srs]+p->c==dis[p->x])
            distante (p->x);
}

void solve ()
{
    int i;
    distante (s);
    for (i=1; i<=n; ++i)
        if (viz[i])
            ++p;
    if (p==n)
        printf ("DA\n");
    else
        printf ("NU\n");
}

int main ()
{
    freopen ("distante.in","r",stdin);
    freopen ("distante.out","w",stdout);
    int i;
    scanf ("%d",&t);
    for (i=1; i<=t; ++i)
    {
        clean ();
        read ();
        solve ();
    }
    return 0;
}