Pagini recente » Cod sursa (job #1867137) | Cod sursa (job #642656) | Cod sursa (job #2904007) | Cod sursa (job #2574891) | Cod sursa (job #1124545)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
struct nod{int frate, cost; nod(int frate, int cost) {this->frate=frate; this->cost=cost;}};
queue <int> q;
int t, n, m, s, d[50001], v[50001];
bool ok;
int main()
{
int i, j, x, y, z;
in>>t;
for(i=1;i<=t;i++)
{
vector <nod> a[50001];
in>>n>>m>>s;
for(j=1;j<=n;j++)
{
in>>v[j];
d[j]=50000000;
}
for(j=1;j<=m;j++)
{
in>>x>>y>>z;
a[x].push_back(nod(y, z));
a[y].push_back(nod(x, z));
}
q.push(s);
d[s]=0;
unsigned int i;
int y;
while(!q.empty())
{
s=q.front();
q.pop();
for(i=0;i<a[s].size();i++)
{
y=a[s][i].frate;
if(d[s]+a[s][i].cost<d[y])
{
d[y]=d[s]+a[s][i].cost;
q.push(y);
}
}
}
j=1;
ok=true;
while(ok && j<=n)
{
if(v[j]!=d[j]) ok=false;
j++;
}
if(ok) out<<"DA"<<"\n";
else out<<"NU"<<"\n";
}
return 0;
}