Cod sursa(job #1307674)

Utilizator BlackBird_v.1.0Stephen Berg BlackBird_v.1.0 Data 2 ianuarie 2015 17:37:05
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Nmax 60013
#define inf 0x3f3f3f3
#define nod first
#define l64 long long
#define cost second
typedef struct celula {
                       int nod;
                       int cost;
                       celula* next;
                       }*lista;
lista G[Nmax];
int i,j,n,m,a,b,c,t,s;
l64 D[Nmax];
l64 sol[Nmax];

void bellmanford(int nod)
{
 for (i=1;i<=n;++i) D[i]=inf;
 queue <int> Q;
 Q.push(nod);
 D[nod]=0;
 while (!Q.empty())
    {
     int v=Q.front(); 
     lista r=G[v];
     while(r)
	     {
          if (D[v]+r->cost<D[r->nod])
		       {
                D[r->nod]=D[v]+r->cost;
                Q.push(r->nod);
                }
          r=r->next;
         }
	Q.pop();
    }    
}
 
void add(int nod,int cost,lista &p)
{
  lista r=new celula;
  r->nod=nod;
  r->cost=cost;
  r->next=p;
  p=r;
}  
 
int main(void)
{
 ifstream in("distante.in");
 ofstream out("distante.out");
 in>>t;
 while(t--)
  {
   memset(D,0,sizeof D);
   memset(sol,0,sizeof sol);
   for (i=1;i<=n;++i) G[i]=NULL;
   in>>n>>m>>s;
   for (i=1;i<=n;++i) in>>sol[i];
   while(m--)
    {
     in>>a>>b>>c;
     add(b,c,G[a]);
     add(a,c,G[b]);
    }
   bellmanford(s);
   int ok=0;
   for (i=1;i<=n;++i) 
     if (D[i]!=sol[i]) ok=1;
    if (!ok) out<<"DA\n";
        else out<<"NU\n";
    }
return 0;
}