Cod sursa(job #82485)

Utilizator gigi_becaliGigi Becali gigi_becali Data 7 septembrie 2007 10:50:03
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
using namespace std;
#include <cstdio>
#include <algorithm>
#include <list>
#define oo 0x3f3f3f3f
#define pb push_back
#define maxn 50001
int d[maxn], d1[maxn];

struct nod { int n, c;nod(){}; nod(int a, int b){n=a;c=b;};};

list<nod>a[maxn];
int n;
int H[maxn*3], nh;

struct cmp{ 
  bool operator()(const int &a, const int &b)const
  {
    if(d[a]>d[b]) return 1;
    return 0;
  }
};

void dijkstra(int s)
{
  memset(d, oo, sizeof(d));
  d[s]=0;
  H[nh++]=s;
  int u;
  list<nod>::iterator i,last;
  while(nh)
    {
      u=H[0];
      pop_heap(H, H+nh, cmp());
      --nh;

      for(i=a[u].begin(), last=a[u].end(); i!=last ;++i)
	if(d[u]+i->c<d[i->n])
	  {
	    d[i->n]=d[u]+i->c;
	    H[++nh]=i->n;
	    push_heap(H, H+nh-1, cmp());
	  }
    }

}

int main()
{
  freopen("distante.in","r",stdin);
  freopen("distante.out","w",stdout);
  int T,m, s, p, q, c;
  scanf("%d\n", &T);
  while(T--)
    {
      scanf("%d %d %d\n", &n, &m,&s);
      for(int i=1;i<=n;++i) scanf("%d ", &d1[i]);
      while(m--)
	{
	  scanf("%d %d %d\n", &p, &q, &c);
	  a[p].pb(nod(q, c));
	  a[q].pb(nod(p, c));
	}
      dijkstra(s);
      int ok=1;
      for(int i=1;i<=n;++i) if(d[i]!=d1[i]) { ok=0;break;}
      if(ok)printf("DA\n");
      else printf("NU\n");
    }

  return 0;
}