Mai intai trebuie sa te autentifici.
Cod sursa(job #1036372)
Utilizator | Data | 19 noiembrie 2013 12:01:44 | |
---|---|---|---|
Problema | Distante | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 1.66 kb |
#include <stdio.h>
using namespace std;
const int N=50001;
const int M=100001;
const int INF=1000000000;
const int NIL=-1;
struct nod
{
int val;
int urm;
int cost;
};
nod a[2*M];
int list[N],d[N],nr,n,m,t,s;
bool e[N];
int main()
{
FILE *in,*out;
in=fopen("distante.in","r");
out=fopen("distante.out","w");
int i,j,x,y,poz,c;
bool corect;
fscanf(in,"%d",&t);
for(j=1;j<=t;j++)
{
fscanf(in,"%d%d%d",&n,&m,&s);
for(i=1;i<=n;i++) fscanf(in,"%d",&d[i]);
nr=0;
for(i=1;i<=n;i++) list[i]=NIL;
for(i=1;i<=m;i++)
{
fscanf(in,"%d%d%d",&x,&y,&c);
a[nr].val=y;
a[nr].urm=list[x];
list[x]=nr;
a[nr].cost=c;
nr++;
a[nr].val=x;
a[nr].urm=list[y];
list[y]=nr;
a[nr].cost=c;
nr++;
}
corect=1;
for(x=1;x<=n;x++) e[x]=0;
for(x=1;x<=n;x++)
{
poz=list[x];
while(poz!=NIL)
{
y=a[poz].val;
if(d[y] + a[poz].cost < d[x])
corect=0;
if(d[x] + a[poz].cost < d[y])
corect=0;
if(d[y] + a[poz].cost == d[x])
e[x]=1;
if(d[x] + a[poz].cost == d[y])
e[y]=1;
poz=a[poz].urm;
}
}
for(x=2;x<=n;x++)
{
if(e[x]==0) corect=0;
}
if(corect) fprintf(out,"DA\n");
else fprintf(out,"NU\n");
}
return 0;
}