Pagini recente » Cod sursa (job #846890) | Cod sursa (job #2561492) | Cod sursa (job #2263170) | Cod sursa (job #2869078) | Cod sursa (job #2040200)
#include <fstream>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int N = 50005, M = 1999999999;
int dist[N], d[N], h[N], poz[N];
struct nod{
int nr,cost;
nod *urm;
}*v[N];
void adaug(int x, int y, int z){
nod *p = new nod;
p->nr = y;
p->cost = z;
p->urm = v[x];
v[x] = p;
}
void upHeap(int f){
while(f/2 && d[h[f]] < d[h[f / 2]]){
swap(h[f], h[f/2]);
poz[h[f]] = f;
poz[h[f / 2]] = f/2;
f /= 2;
}
}
void downHeap(int t, int l){
int f = 0;
while(t != f){
f = t;
if(f * 2 <= l && d[h[t]] > d[h[f * 2]])
t = f*2;
if(f * 2 + 1 <= l && d[h[t]] > d[h[f * 2 + 1]])
t = f*2+1;
swap(h[f],h[t]);
poz[h[f]] = f;
poz[h[t]] = t;
}
}
void dijkstra(int ns, int n){
int l = 0, rad, c, val;
nod *nou = new nod;
d[ns] = 0;
poz[ns] = 1;
h[++l] = ns;
while(l){
rad = h[1];
swap(h[1], h[l--]);
poz[h[1]] = 1;
downHeap(1,l);
nou = v[rad];
while(nou){
c = nou->cost;
val = nou->nr;
if(d[val] > d[rad] + c){
d[val] = d[rad] + c;
if(poz[val])
upHeap(poz[val]);
else{
h[++l] = val;
poz[h[l]] = l;
upHeap(l);
}
}
nou = nou->urm;
}
}
}
void reinitialise(int n){
for(int i=1;i<=n;i++){
d[i] = M;
h[i] = poz[i] = 0;
v[i] = NULL;
}
}
bool identici(int n, int a[], int b[]){
for(int i=1;i<=n;i++)
if(a[i] != b[i])
return false;
return true;
}
int main()
{
int t,n,m,ns,x,y,z;
in>>t;
for(int q=1;q<=t;q++){
in>>n>>m>>ns;
for(int i=1;i<=n;i++)
in>>dist[i];
reinitialise(n);
for(int i=1;i<=m;i++){
in>>x>>y>>z;
adaug(x,y,z);
adaug(y,x,z);
}
dijkstra(ns,n);
if(identici(n,d,dist))
out<<"DA\n";
else
out<<"NU\n";
}
in.close();
out.close();
return 0;
}