Pagini recente » Cod sursa (job #1482211) | Cod sursa (job #2896256) | Cod sursa (job #1347240) | Cod sursa (job #2264565) | Cod sursa (job #2830905)
#include <fstream>
#include <algorithm>
#include <queue>
#include <iterator>
#include <vector>
#include <unordered_set>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
int nrGrafuri, nrNoduri, nrMuchii, sursa;
bool check;
int distanteDate[50002], distante[50002];
struct muchie {
int destinatie, cost;
muchie(int x, int y) : destinatie(x), cost(y) {};
};
vector<vector<muchie>> listaDeAdiacenta;
void dijkstra(int nodPlecare) {
const int maxi = 1000000002;
vector<bool> vizitat(50002, false);
struct Node {
int node, weight;
Node(int n, int w) : node(n), weight(w) {};
bool operator<(const Node &ob) const {
return weight > ob.weight;
}
};
priority_queue<Node> minHeap;
for (int i = 1; i <= nrNoduri; i++)
distante[i] = maxi;
distante[nodPlecare] = 0;
minHeap.push(Node(nodPlecare, 0));
int nodCurent;
while (minHeap.size() > 0) {
if (vizitat[minHeap.top().node])
minHeap.pop();
else {
nodCurent = minHeap.top().node;
for (auto i: listaDeAdiacenta[nodCurent])
if (distante[i.destinatie] > distante[nodCurent] + i.cost) {
distante[i.destinatie] = distante[nodCurent] + i.cost;
minHeap.push(Node(i.destinatie, distante[i.destinatie]));
}
vizitat[nodCurent] = true;
}
}
for (int i = 1; i <= nrNoduri; i++)
if (distante[i] == maxi)
distante[i] = 0;
}
int main() {
listaDeAdiacenta = vector<vector<muchie>>(50000, vector<muchie>());
fin >> nrGrafuri;
for (int i = 1; i <= nrGrafuri; i++) {
fin >> nrNoduri >> nrMuchii >> sursa;
for (int j = 1; j <= nrNoduri; j++)
fin >> distanteDate[j];
for (int i = 1; i <= nrMuchii; i++) {
int x, y, c;
fin >> x >> y >> c;
listaDeAdiacenta[x].push_back(muchie(y, c));
listaDeAdiacenta[y].push_back(muchie(x, c));
}
dijkstra(sursa);
check = true;
for (int j = 1; j <= nrNoduri; j++)
if (distante[j] != distanteDate[j]) {
check = false;
break;
}
if (check)
fout << "DA\n";
else
fout << "NU\n";
}
fin.close();
fout.close();
return 0;
}