Cod sursa(job #255647)

Utilizator cameleonGeorgescu Dan cameleon Data 10 februarie 2009 09:35:48
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include<stdio.h>
#define max 500
long d[max], n, m, s, t;
struct elem
{       long v, c;
	elem *urm;
}	*a[max], *q;
FILE *f, *g;

void read()
{       long i, x, y, cs;
	fscanf(f, "%ld%ld%ld", &n, &m, &s);
	for(i=1; i<=n; i++)
		fscanf(f, "%ld", &d[i]);
	for(i=0; i<m; i++)
	{	fscanf(f, "%ld%ld%ld", &x, &y, &cs);
		q=new elem;
		q->c=cs; q->v=y;
		q->urm=a[x];
		a[x]=q;
		q=new elem;
		q->c=cs; q->v=x;
		q->urm=a[y];
		a[y]=q;
	}

/*	for(i=1; i<=n; i++)
	{	q=a[i];
		while(q)
			{cout<<q->v<<" ";
			q=q->urm;
			}
		cout<<endl;
	}
*/
}

int solve()
{       long i;
	if(d[s]!=0)
		return 0;
/*	q=a[s];
	while(q)
	{       if(d[q->v]>q->c)
			return 0;
		q=q->urm;
	}*/

	for(i=1; i<=n; i++)
		{	q=a[i];
			while(q)
			{       if(d[i]>d[q->v]+q->c)
					return 0;
				q=q->urm;
			}
		}
	}
	return 1;
}

int main()
{       int k, l;
	f=fopen("distante.in", "r");
	g=fopen("distante.out", "w");
	fscanf(f, "%ld", &t);
	for(l=0; l<t; l++)
	{
		read();
		k=solve();
		if(k==0)
			fprintf(g, "%s\n", "NU");
		else
			fprintf(g, "%s\n", "DA");
	}

	fclose(g);
	return 0;
}