Cod sursa(job #555080)

Utilizator Cristy94Buleandra Cristian Cristy94 Data 15 martie 2011 11:43:33
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#define Nmax 50010
#define dest first
#define costs second
using namespace std;

int c[Nmax],min_c[Nmax],N;
bool ok=1,inheap[Nmax];

struct cmp{
	bool operator() (const int&a,const int&b)const{
		return (min_c[a] > min_c[b]);
	}
};

vector <pair <int,int> > a[Nmax];
priority_queue <int,vector <int>,cmp> q;



void dijkstra_check(){
	while(!q.empty()){
		int nod=q.top();
		int cost=min_c[nod];
		q.pop();
		inheap[nod]=0;
		
		for(unsigned int i=0;i<a[nod].size();++i){
			
				if(cost+a[nod][i].costs < min_c[a[nod][i].dest]){
					
					min_c[a[nod][i].dest]=cost+a[nod][i].costs;
				
					if(!inheap[a[nod][i].dest])
						q.push(a[nod][i].dest);			
				}
		}
	}
	
	for(int i=1;i<=N;++i)
		if(min_c[i]!=c[i]){
			ok=0;
			break;
		}
}
int main(){
	freopen("distante.in","r",stdin);
	freopen("distante.out","w",stdout);
	int T;
	for(scanf("%d",&T); T; --T){
		
		int M,S;
		scanf("%d%d%d",&N,&M,&S);
		
		memset(inheap,0,sizeof(inheap));
		memset(min_c,0x3f3f3f,sizeof(min_c));
		ok=1;
		
		
		for(int i=1;i<=N;++i){
			scanf("%d",&c[i]);
			a[i].clear();
		}
		
		for(int i=1;i<=M;++i){
			int x,y,c;
			scanf("%d%d%d",&x,&y,&c);
			a[x].push_back(make_pair(y,c));
			a[y].push_back(make_pair(x,c));
	     }
		    min_c[S]=0;
			q.push(S);
		    dijkstra_check();
			if(ok)
				printf("DA\n");
			else
				printf("NU\n");
	}
return 0;
}