Pagini recente » Cod sursa (job #376887) | Cod sursa (job #2634235) | Cod sursa (job #1603930) | Cod sursa (job #1705631) | Cod sursa (job #282428)
Cod sursa(job #282428)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NMAX 50001
#define oo 0x3f3f3f3f
FILE *f=fopen("distante.in","r");
FILE *g=fopen("distante.out","w");
struct nod{
int nd;
int ct;
nod* urm;
/*nod(){
nd=0;ct=0;
urm = NULL;
}*/
};
nod * G[NMAX];
int D[NMAX];
int t,S,n,m,lh;
int d[NMAX],h[NMAX],p[NMAX];
inline void swap(int &a, int &b){ int aux= a; a=b; b=aux; }
void up(int x){
int t;
while(x<=lh){
t = x<<1;
if(d[h[x]] > d[h[t]]){
swap(h[x],h[t]);
p[x]=t;
p[t]=x;
x=t;
}else return;
}
}
void down(int x){
int f;
while(x>1){
f=x>>1;
if(f<=lh){
if(f+1<=lh)
if(d[h[f]] > d[h[f+1]]) f++;
}else return;
if(d[h[x]] < d[h[f]]){
swap(h[x],h[f]);
p[x]=f;
p[f]=x;
x=f;
}else return;
}
}
inline void push(int x){
h[++lh] = x;
p[h[lh]] = lh;
up(lh);
}
inline void pop() {
swap(h[1],h[lh--]);
p[h[1]]=1;
down(1);
}
void dijkstra(){
memset(d,oo,sizeof(d));
memset(p,-1,sizeof(p));
memset(h,0,sizeof(h));
lh = 0;
d[S]=0;
push(S);
int min;
while(lh){
min = h[1];
pop();
nod *q = new nod;
q = G[min];
for(;q;q=q->urm){
if(d[q->nd] > d[min] + q->ct ){
d[q->nd] = d[min] + q->ct;
if(p[q->nd]==-1) push(q->nd);
else down(p[q->nd]);
}
}
}
}
int ok(){
for(int i=1;i<=n;i++) if(D[i]!=d[i]) return 0;
return 1;
};
inline void ADD(nod*& p, int x, int c){
nod *q = new nod;
q->urm = p;
q->nd = x;
q->ct = c;
p=q;
}
void date_graf(){
int i,x,y,c;
nod* q= new nod;
nod* p= new nod;
for(i=1;i<=n;i++){
q = G[i];
while(q){
p=q->urm;
delete(q);
q=p;
}
}
fscanf(f,"%d%d%d",&n,&m,&S);
for(i=1;i<=n;i++) fscanf(f,"%d",D+i);
for(i=1;i<=n;i++){
fscanf(f,"%d%d%d",&x,&y,&c);
ADD(G[x],y,c);
ADD(G[y],x,c);
}
}
int main(){
fscanf(f,"%d",&t);
for( ; t -- ; ){
date_graf();
dijkstra();
if(ok()) fprintf(g,"DA\n");
else fprintf(g,"NU\n");
}
return 0;
}