Pagini recente » Cod sursa (job #1135433) | Cod sursa (job #558759) | Cod sursa (job #1884837) | Cod sursa (job #1745630) | Cod sursa (job #2572887)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int NMax = 250.000;
const int oo = (1 << 30);
int N, M;
int D[NMax];
bool InCoada[NMax];
vector < pair <int,int> > G[NMax];
queue <int> bee;
struct compara
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue<int, vector<int>, compara> Coada;
int sursa;
void Citeste()
{
fin >> N >> M>>sursa;
for(int j=1;j<=N;j++)
{
int u;
fin>>u;
bee.push(u);
}
for(int i = 1; i <= M; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y,c));
G[y].push_back(make_pair(x,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()
{int ok=0;
for(int i = 1; i <= N&&!ok; i++)
{
if(D[i] != bee.front())
ok=1;
bee.pop();
}
if (ok)
fout<<"NU"<<'\n';
else
fout<<"DA"<<'\n';
}
int main()
{int t;
fin>>t;
while(t)
{
Citeste();
Dijkstra(sursa);
Afiseaza();
t--;
}
}