Cod sursa(job #1804596)

Utilizator GabiTulbaGabi Tulba-Lecu GabiTulba Data 12 noiembrie 2016 19:42:26
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define  MaxN 50005
using namespace std;
 
FILE *IN,*OUT;
int N,M,S,T,v[MaxN],Obt,X,Y,Z;
bool obtain[MaxN];
int main()
{
    IN=fopen("test.in","r");
    OUT=fopen("test.out","w");

	fscanf(IN,"%d",&T);
	for(int t=1;t<=T;t++)
	{
		fscanf(IN,"%d%d%d",&N,&M,&S);
		memset(obtain,0,sizeof obtain);
		for(int i=1;i<=N;i++)
			fscanf(IN,"%d",&v[i]);
		if(v[S]!=0)
		{
			fprintf(OUT,"NU\n");
		}
		else
		{
			Obt=1,obtain[S]=true;
			for(int i=1;i<=M;i++)
			{
				fscanf(IN,"%d%d%d",&X,&Y,&Z);
				if(v[X]+Z<v[Y]||v[Y]+Z<v[X])
				{
					Obt=-1;
					break;
				}
				else if(!obtain[Y]&&v[X]+Z==v[Y])
					obtain[Y]=true,Obt++;
				else if(!obtain[X]&&v[Y]+Z==v[Y])
					obtain[X]=true,Obt++;
			}
			if(Obt==-1)
				fprintf(OUT,"NU\n");
			else if(Obt==N)
				fprintf(OUT,"DA\n");
			else
				fprintf(OUT,"NU\n");
			
		}
	}
	return 0;
}