Pagini recente » Cod sursa (job #2590247) | Cod sursa (job #247883) | Cod sursa (job #851004) | Cod sursa (job #2241669) | Cod sursa (job #3152073)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
int t, n, m, s, w[50005], a, b, c, cost[50005];
vector<pair<int,int>> v[100005];
priority_queue<pair<int,int>> q;
void dijkstra(int nod) {
for (int i = 1; i <= n; i++) cost[i] = INT_MAX;
cost[nod] = 0;
q.push({ 0,nod });
while (!q.empty()) {
pair <int, int> cr = q.top();
q.pop();
if (cost[cr.second] != -cr.first) continue;
for (auto el : v[cr.second]) {
if (cost[el.second] > -cr.first + el.first) {
cost[el.second] = -cr.first + el.first;
q.push({-cost[el.second], el.second});
}
}
}
}
ifstream fin("distante.in");
ofstream fout("distante.out");
int main() {
fin >> t;
for (int k = 1; k <= t; k++) {
fin >> n >> m >> s;
for (int i = 1; i <= n; i++) v[i].clear();
for (int i = 1; i <= n; i++) {
fin >> w[i];
}
for (int i = 1; i <= m; i++) {
fin >> a >> b >> c;
v[a].push_back({ c,b });
v[b].push_back({ c,a });
}
dijkstra(s);
int ok = 1;
for (int i = 1; i <= n; i++) {
if (cost[i] != w[i]) {
ok = 0;
break;
}
}
if (ok) fout << "DA";
else fout << "NU";
}
}