Pagini recente » Profil horatiuros | Cod sursa (job #1201495) | Cod sursa (job #859014) | Cod sursa (job #2453256) | Cod sursa (job #2828180)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
typedef pair<int, int> pii;
const int nMax = 50001;
const int inf = 1 << 30;
int n, m;
vector<pii> ad[nMax];
bool vis[nMax];
int dist0[nMax], dist[nMax];
int read() {
int start, x, y, cost;
fin >> n >> m >> start;
for (int i = 1; i <= n; ++i) {
fin >> dist0[i];
}
for (int i = 1; i <= m; ++i) {
fin >> x >> y >> cost;
ad[x].emplace_back(y, cost);
ad[y].emplace_back(x, cost);
}
return start;
}
void dijkstra(int start) {
priority_queue<pii, vector<pii>, greater<pii>> heap;
for (int i = 1; i <= n; ++i) {
dist[i] = inf;
}
dist[start] = 0;
heap.push({0, start});
while (!heap.empty()) {
int current = heap.top().second; heap.pop();
if (!vis[current]) {
vis[current] = true;
if (dist[current] != dist0[current]) {
break;
}
for (auto &w : ad[current]) {
if (dist[w.first] > dist[current] + w.second) {
dist[w.first] = dist[current] + w.second;
heap.push({dist[w.first], w.first});
}
}
}
}
}
void doQuery() {
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
for (int i = 1; i < nMax; ++i) {
ad[i].clear();
}
int start = read();
dijkstra(start);
for (int i = 1; i <= n; ++i) {
if (dist[i] != 0) {
if (dist[i] != dist0[i]) {
fout << "NU\n";
return;
}
}
}
fout << "DA\n";
}
int main() {
int queries;
fin >> queries;
for (int i = 1; i <= queries; ++i) {
doQuery();
}
fin.close();
fout.close();
return 0;
}