Pagini recente » Cod sursa (job #368877) | Cod sursa (job #2815119) | Cod sursa (job #479152) | Cod sursa (job #1158237) | Cod sursa (job #3002519)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
string file = "distante";
ifstream cin (file + ".in");
ofstream cout (file + ".out");
struct muchie{
int nod,cost;
};
const int N = 50000, INF = 50000001;
vector <muchie> L[N+1];
vector <int> d(N+1);
int n,m,k,v[N], h[10 + N], poz[N+1],nh;
inline void schimb(int x, int y)
{
swap(h[x],h[y]);
swap(poz[h[x]],poz[h[y]]);
}
inline void urca(int p)
{
while (p>1 && d[h[p/2]] > d[h[p]])
{
schimb(p/2,p);
p/=2;
}
}
inline void coboara(int p)
{
while (true)
{
int bun = p, fs = p*2, fd=p*2+1;
if (fs <= nh && d[h[bun]] > d[h[fs]])
{
bun = fs;
}
if (fd <= nh && d[h[bun]] > d[h[fd]])
{
bun = fd;
}
if (bun == p)
break;
schimb(bun,p);
p = bun;
}
}
inline void adauga(int nod)
{
h[++nh] = nod;
poz[nod] = nh;
urca(nh);
}
inline void sterge(int p, int nod)
{
schimb(p,nh);
poz[nod] = 0;
nh--;
urca(p);
coboara(p);
}
void bfs (int x)
{
d[x] = 0;
h[++nh] = x;
while (nh)
{
x = h[1];
sterge(1,h[1]);
for (auto y : L[x])
{
if (d[y.nod] > d[x] + y.cost)
{
d[y.nod] = d[x] + y.cost;
if (poz[y.nod] == 0)
{
adauga(y.nod);
}
else
{
urca(poz[y.nod]);
}
}
}
}
}
int main()
{
int t,a,b,c;
cin >> t;
while (t--)
{
cin >> n >> m >> k;
for (int i=1; i<=N; i++)
{
L[i].clear();
h[i] = 0;
poz[i] = 0;
d[i] = INF;
}
for (int i=1; i<=n; i++)
cin >> v[i];
while (m--)
{
cin >> a >> b >> c;
L[a].push_back({b,c});
L[b].push_back({a,c});
}
bfs(k);
bool ok = 1;
for (int i=1; i<=n; i++)
{
if (v[i] != d[i])
{
ok = 0;
break;
}
}
cout << (ok == 1 ? "DA\n" : "NU\n");
}
}