Pagini recente » Cod sursa (job #1017579) | Cod sursa (job #2271650) | Cod sursa (job #1304750) | Cod sursa (job #3150943) | Cod sursa (job #4296)
Cod sursa(job #4296)
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 1<<16
#define oo 0x3f3f3f3f
using namespace std;
int H[maxn], n, m, T, d[maxn], d1[maxn];
struct nod{int nd, w; nod(int _nd, int _w){nd=_nd; w=_w;};};
vector<vector<nod> >a;
bool operator<(const nod &a, const nod &b)
{
if(a.w<b.w) return 1;
//if(a.w==b.w) if(a.nd<b.nd) return 1;
return 0;
}
bool operator>(const nod &a, const nod &b)
{
if(a.w>b.w) return 1;
return 0;
}
void dijkstra(int s)
{
priority_queue<nod>Q;
bool viz[maxn];
int u, i;
vector<nod>::iterator it;
memset(viz, 0, sizeof(viz));
memset(d, oo, sizeof(d));
Q.push(nod(s, 0));
d[s]=0;
int nh=n;
while(Q.size() && nh)
{
u=Q.top().nd; Q.pop();
if(viz[u])continue;
viz[u]=1;
nh--;
for(it=a[u].begin();it!=a[u].end();++it)
if(d[u]+it->w<d[it->nd])
{
d[it->nd]=d[u]+it->w;
Q.push(*it);
}
}
}
int main()
{
freopen("distante.in", "r", stdin);
freopen("distante.out", "w", stdout);
int i, j,p, q, w, s;
scanf("%d", &T);
for(i=1;i<=T;i++)
{
a.clear();
scanf("%d %d %d\n", &n, &m, &s);
a.resize(n+1);
for(j=1;j<=n;j++) scanf("%d ", d1+j);
for(j=1;j<=m;j++)
{
scanf("%d %d %d\n", &p, &q, &w);
a[p].push_back(nod(q, w));
a[q].push_back(nod(p, w));
}
dijkstra(s);
int ok=1;
// for(j=1;j<=n;j++) printf("%d ", d[j]); printf("\n");
for(j=1;j<=n;j++) if(d1[j]!=d[j]){ ok=0; break;}
if(ok) printf("DA\n");
else printf("NU\n");
}
return 0;
}