Pagini recente » Cod sursa (job #1090386) | Cod sursa (job #1587377) | Cod sursa (job #1894400) | Cod sursa (job #877263) | Cod sursa (job #1118568)
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
fstream f("distante.in",ios::in);
fstream g("distante.out",ios::out);
const int nmax=50005,INF=0x3f3f3f3f;
int k,t;
void rezolvare()
{
vector < pair <int,int> > a[nmax];
queue <int> q;
int viz[nmax],dist[nmax],rez[nmax];
int n,m,nc,i,lafel,x1,y,c,nr;
memset(viz,0,sizeof(viz));
memset(dist,INF,sizeof(dist));
memset(rez,0,sizeof(rez));
f>>n>>m>>nc;
for (i=1;i<=n;i++) f>>rez[i];
for (i=1;i<=m;i++)
{
f>>x1>>y>>c;
a[x1].push_back(make_pair(y,c));
a[y].push_back(make_pair(x1,c));
}
q.push(nc);
viz[nc]=1;
dist[nc]=0;
while (!q.empty())
{
nc=q.front();
q.pop();
viz[nc]=0;
for (vector <pair <int,int> >::iterator it=a[nc].begin();it!=a[nc].end();++it)
if (dist[nc]+it->second<dist[it->first])
{
dist[it->first]=dist[nc]+it->second;
if (!viz[it->first])
{
viz[it->first]=1;
q.push(it->first);
}
}
}
lafel=1;
for (i=1;i<=n;i++) if (dist[i]==INF) dist[i]=0;
for (i=1;i<=n;i++)
{
if (dist[i]!=rez[i]) {
lafel=0;
break;
}
}
if (lafel) g<<"DA\n";
else g<<"NU\n";
}
int main()
{
f>>t;
for (k=1;k<=t;k++) rezolvare();
f.close();g.close();
return 0;
}