Pagini recente » Cod sursa (job #2535257) | Cod sursa (job #41927) | Cod sursa (job #3173667) | Cod sursa (job #1000472) | Cod sursa (job #1376642)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define n1 first
#define c1 second
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
const int inf = 0x3f3f3f3f;
queue <int > q;
vector < pair <int, int> >v[50000];
int t,sol[50000],sol1[50000],n,m,k,a,b,c;
bool ok;
void bfs(int s)
{
q.push(s);
for(int i=1; i<=n; i++)
sol[i]=inf;
sol[s]=0;
while(!q.empty())
{
int curent=q.front();
q.pop();
for(int i=0; i<v[curent].size(); i++)
if(sol[v[curent][i].n1]>sol[curent]+v[curent][i].c1)
{
q.push(v[curent][i].n1);
sol[v[curent][i].n1]=sol[curent]+v[curent][i].c1;
}
}
}
int main()
{
f>>t;
for(int i=1; i<=t; i++)
{
f>>n>>m>>k;
for(int j=1; j<=n; j++)
f>>sol1[j];
ok=true;
if(sol1[k]!=0)g<<"NU\n";
else
{
for(int j=1; j<=m; j++)
{
f>>a>>b>>c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
bfs(k);
for(int j=1; j<=n; j++)
{
v[j].clear();
if(sol1[j]!=sol[j]){ok=false;break;}
}
if(ok==false)g<<"NU\n";
else g<<"DA\n";
}
}
return 0;
}