Pagini recente » Cod sursa (job #1539112) | Cod sursa (job #3189871) | Cod sursa (job #2904226) | Cod sursa (job #1256018) | Cod sursa (job #2423843)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int main() {
int numar_grafuri, n, m, s;
f >> numar_grafuri;
for (int j = 0; j < numar_grafuri; j++) {
f >> n >> m >> s;
vector <int> viz(n + 1, 0);
vector <int> dist(n + 1, 1002);
vector < vector< pair<int, int>>> G(n + 1);
queue <int> Q;
for (int i = 1; i <= n; i++)
f >> dist[i];
for (int i = 0; i < m; i++) {
int x, y, cost;
f >> x >> y >> cost;
G[x].push_back(make_pair(cost, y));
G[y].push_back(make_pair(cost, x));
}
Q.push(s);
viz[s] = 1;
int cnt = n;
while (!Q.empty()) {
int nodCurent = Q.front();
Q.pop();
cnt--;
for (auto nod : G[nodCurent]) {
if (dist[nodCurent] + nod.first == dist[nod.second]) {
viz[nod.second] = 1;
Q.push(nod.second);
}
}
}
if (cnt > 0) g << "NU\n";
else g << "DA\n";
}
return 0;
}