Pagini recente » Cod sursa (job #1278455) | Cod sursa (job #2119951) | Cod sursa (job #1592682) | Cod sursa (job #1930164) | Cod sursa (job #2612253)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMax = 50005;
const int oo = (1 << 30);
int N, M,S;
int D[NMax],DS[NMax];
bool InCoada[NMax];
vector < pair < int, int > > G[NMax];
struct comparaDistante
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue < int, vector < int >, comparaDistante> Coada;
void Citeste()
{
fin >> N >> M>>S;
for(int i=1;i<=N;i++){
fin >>DS[i];
}
for(int i = 1; i <= M; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y,c));
}
}
void Dijkstra(int nodStart)
{
for(int i = 1; i <= N; i++)
D[i] = oo;
D[nodStart] = 0;
Coada.push(nodStart);
InCoada[nodStart] = 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;
}
}
}
}
}
void Afiseaza()
{
bool da=0;
for(int i = 2; i <= N; i++)
{
if(D[i] != DS[i]){
fout <<"NU"<<"\n";
da=1;
break;
}
}
if(!da)fout <<"DA"<<"\n";
}
int main()
{
int tt;
fin >>tt;
while(tt--){
Citeste();
Dijkstra(S);
Afiseaza();
}
}