Pagini recente » Cod sursa (job #3271962) | Cod sursa (job #424903) | Cod sursa (job #2692797) | Cod sursa (job #815294) | Cod sursa (job #2984998)
#include <fstream>
#include<vector>
#include<queue>
#include<algorithm>
#include <cmath>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
const int oo=1<<30;
int n,m,tot,D[50001];
struct compara{
bool operator()(int x,int y)
{
return D[x]>D[y];
}
};
priority_queue<int, vector<int>, compara> Coada;
int main()
{
f>>tot;
while(tot--)
{
int start;
f>>n>>m>>start;
int sol[n+1];
for(int i=1;i<=n;++i)
f>>sol[i],D[i]=oo;
int x,y,c;
bool InCoada[n+1];
vector < pair <int,int> > G[n+1];
while(m--)
{
f>>x>>y>>c;
G[x].push_back(make_pair(y,c));
G[y].push_back(make_pair(x,c));
}
D[start]=0;
Coada.push(start);
InCoada[start] = true;
while(!Coada.empty())
{
int nodCurent = Coada.top();
Coada.pop();
InCoada[nodCurent] = false;
for(size_t i = 0; i < G[nodCurent].size(); i++)
{
int Vecin = G[nodCurent][i].first;
int Cost = G[nodCurent][i].second;
if(D[nodCurent] + Cost < D[Vecin])
{
D[Vecin] = D[nodCurent] + Cost;
if(InCoada[Vecin] == false)
{
Coada.push(Vecin);
InCoada[Vecin] = true;
}
}
}
}
bool ok=1;
for(int i=1;i<=n ;++i)
if(D[i]!=sol[i])
{ok=0;break;}
if(ok)
g<<"DA\n";
else g<<"NU\n";
}
f.close();
g.close();
return 0;
}