Cod sursa(job #560436)

Utilizator mytzuskyMihai Morcov mytzusky Data 18 martie 2011 14:55:56
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <iostream>
#include <string.h>
#include <fstream>
#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];
    g[x]=aux;
}

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);

    ifstream f("distante.in");
    ofstream gg("distante.out");

    f >> t;

    for(int y=0;y<t;y++)
    {





        f >>n >>m >>s;

        for(int h=1;h<=n;h++)
            f >> ver[h];

        for(int k=0;k<m;k++)
        {
            int x,y,z;
            f >> x>>y>>z;

            add(x,y,z);
        }
     //   memset(dist,oo,sizeof(dist));
       // memset(viz,0,sizeof(viz));

        for(int ii=0;ii<=n;ii++) dist[ii]=9999, viz[ii]=0;
        Q.push(s);
        viz[s] = 1;
        dist[s]= 0;




        while( ! Q.empty())
        {
            int nd = Q.front();
            Q.pop();
            viz[nd]=0;


            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))
            gg << "DA\n";
        else
            gg << "NU\n";
    }
}


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