Cod sursa(job #560216)

Utilizator mytzuskyMihai Morcov mytzusky Data 18 martie 2011 13:08:48
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <iostream>
#include <stdio.h>
#include <queue>

#define Nmax 50001
#define oo 99999
using namespace std;

int t,n,m,s,viz[Nmax], dist[Nmax], ver[Nmax];

queue <int> Q;


struct nod {
    int inf, cost;
    nod *urm;
} *g[Nmax];

void Add(int x, int y, int z)
{
    nod *aux = new nod;

    aux->inf = y;
    aux->cost= z;
    aux->urm =g[x];
    aux = g[x];
}

int Verificare(int d[Nmax])
{
    for(int i=1;i<n;i++)
        if(ver[i] != d[i])
            return 0;
    return 1;
}


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

    scanf("%d",&t);
    for(int y=0;y<t;y++)
    {
        memset(dist,oo,sizeof(dist));
        memset(viz,0,sizeof(viz));


        scanf("%d %d %d",&n,&m,&s);
        for(int h=1;h<=n;h++)
            scanf("%d ",&ver[h]);
        for(int k=0;k<m;k++)
        {
            int x,y,z;
            scanf("%d %d %d\n",&x,&y,&z);
            Add(x,y,z);
        }

        Q.push(s);
        viz[s] = 1;
        dist[s]= 0;

        while(! Q.empty())
        {
            int nd = Q.front();
            Q.pop();
            viz[nd]=0;
           // cout << "nd=" << nd <<endl;

            for(nod *p=g[nd];p;p=p->urm)
            {
                if(dist[nd] + p->cost < dist[p->inf])
                {
                    dist[p->inf] = dist[nd] + p->cost;

                    if(viz[p->inf]==0)
                    {
                        Q.push(p->inf);
                        viz[p->inf]=1;
                    }
                }
            }
        }
      //  for(int i=1;i<=n;i++)
     //      cout << dist[i]<<" ";
     //   cout << endl;
        if(Verificare(dist))
            cout << "DA\n";
        else
            cout << "NU\n";
    }
}


int main()
{
    Citire();
    return 0;
}