Pagini recente » Cod sursa (job #2787579) | Cod sursa (job #3222046) | Profil Ana-Malina | Cod sursa (job #3222045) | Cod sursa (job #2169487)
#include <cstdio>
#include <vector>
#include <algorithm>
#define N 50002
#define Inf 2000000000
using namespace std;
int q, n, m, k, nod;
int d[N],pos[N],h[N],minim[N];
vector<pair<int,int>>A[N];
void upheap(int x)
{
int tata, what=x;
while(what>1)
{
tata=what/2;
if(d[h[tata]]>d[h[what]])
{
pos[h[tata]]=what;
pos[h[what]]=tata;
swap(h[tata],h[what]);
what=tata;
}
else
return;
}
}
void downheap(int x)
{
int f, what=x;
while(what<=k)
{
f=what;
if(what*2<=k)
{
f=what*2;
if(f+1<=k)
{
if(d[h[f+1]]<d[h[f]])
{
f++;
}
}
}
else
return;
if(d[h[what]]>d[h[f]])
{
pos[h[what]]=f;
pos[h[f]]=what;
swap(h[what],h[f]);
what=f;
}
else
return;
}
}
void dijkstra_heap()
{
int equals=1;
d[nod]=0;
pos[nod]=1;
h[++k]=nod;
while(k)
{
int pnod=h[1];
swap(h[1],h[k]);
pos[h[1]]=1;
k--;
downheap(1);
for(auto w:A[pnod])
{
if(d[w.first]>d[pnod]+w.second)
{
d[w.first]=d[pnod]+w.second;
/*if(d[w.first]<=minim[w.first])
{
if(d[w.first]==minim[w.first])
{
equals++;
}
else
{
printf("NU\n");
return;
}
}*/
if(pos[w.first]!=-1)
{
upheap(pos[w.first]);
}
else
{
h[++k]=w.first;
pos[h[k]]=k;
upheap(k);
}
}
}
}
/*if(equals==n)
{
printf("DA\n");
}
else
{
printf("NU\n");
}*/
bool ok=1;
for(int i=1; i<=n; i++)
{
if(d[i]!=minim[i])
{
ok=0;
break;
}
}
if(ok)
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
int x,y,c,i;
scanf("%d", &q);
while(q)
{
q--;
scanf("%d%d%d", &n, &m, &nod);
for(i=1; i<=n; i++)
{
scanf("%d ", &minim[i]);
A[i].clear();
d[i]=Inf;
pos[i]=-1;
}
for(i=1; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &c);
A[x].push_back({y,c});
A[y].push_back({x,c});
}
dijkstra_heap();
}
return 0;
}