Cod sursa(job #799737)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 19 octombrie 2012 22:14:01
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include<cstdio>
#include<vector>
#include<deque>
#include<algorithm>
#include<cstring>
using namespace std;
int ok,n,i,j,k,a,b,m,rez,T,cost[50005],s[50005],x,y;
vector<pair<int,int> >v[50005];
deque<int>c;
bool viz[50005];

int bfs(int x)
{
	memset(viz,false,sizeof(viz));
	memset(cost,0,sizeof(cost));
	c.push_back(x);viz[x]=1;cost[x]=0;
	while (!c.empty())
	{
		x=c.front();
		for (j=0;j<v[x].size();j++) if (viz[v[x][j].first]==0)
		{
			a=v[x][j].first,b=v[x][j].second;
			c.push_back(a);viz[a]=1;
			cost[a]=cost[x]+b;
		}else if (cost[v[x][j].first]>cost[x]+v[x][j].second) cost[v[x][j].first]=cost[x]+v[x][j].second,c.push_back(v[x][j].first); 
		c.pop_front();
	}
	return 0;
}

int main()
{
	freopen("distante.in","r",stdin);
	freopen("distante.out","w",stdout);
	scanf("%d ",&T);
	for (i=1;i<=T;i++)
	{
		memset(s,0,sizeof(s));
		scanf("%d %d %d",&n,&m,&x);
		j=1;
		while (!v[j].empty()) v[j].pop_back();
		for (j=1;j<=n;j++) scanf("%d",&s[j]);
		for (j=1;j<=m;j++) scanf("%d %d %d",&a,&b,&k),v[a].push_back(make_pair(b,k)),v[b].push_back(make_pair(a,k));
				bfs(x);        ok=0;
		for (j=1;j<=n;j++) { if (s[j]!=cost[j])  ok=1;v[j].clear();}
		if (ok==0) printf("DA\n");else printf("NU\n");
		
	}
	return 0;
}