Pagini recente » Cod sursa (job #2770923) | Cod sursa (job #2694616) | Cod sursa (job #2845023) | Cod sursa (job #2698916) | Cod sursa (job #3219916)
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <utility>
#include <vector>
#include <queue>
typedef std::pair<int32_t, int32_t> pair;
typedef std::vector<pair> vector;
typedef std::priority_queue<pair, vector, std::greater<pair>> queue;
const int32_t MAX_N = 50000;
const int32_t MAX_COST = 1000000000;
int32_t res[MAX_N];
int32_t cost[MAX_N];
vector adj[MAX_N];
queue q;
int main() {
std::ifstream fin("distante.in");
std::ofstream fout("distante.out");
int32_t t;
fin >> t;
while(t--) {
int32_t n, m, s;
fin >> n >> m >> s;
--s;
for(int32_t i = 0; i != n; ++i)
fin >> res[i];
for(int32_t i = 0; i != m; ++i) {
int32_t x, y, c;
fin >> x >> y >> c;
--x; --y;
adj[x].push_back({ y, c });
adj[y].push_back({ x, c });
}
for(int32_t i = 0; i != n; ++i)
cost[i] = MAX_COST;
cost[s] = 0;
q.push({ 0, s });
while(!q.empty()) {
pair val = q.top();
q.pop();
if(cost[val.second] < val.first)
continue;
for(pair next : adj[val.second]) {
int32_t newCost = val.first + next.second;
if(newCost >= cost[next.first])
continue;
cost[next.first] = newCost;
q.push({ newCost, next.first });
}
}
bool equal = true;
for(int32_t i = 0; i != n && equal; ++i)
equal = cost[i] == res[i];
if(equal) {
fout << "DA\n";
} else {
fout << "NU\n";
}
for(int32_t i = 0; i != n; ++i)
adj[i].clear();
}
fin.close();
fout.close();
return 0;
}