Pagini recente » Cod sursa (job #3286934) | Cod sursa (job #3355718) | Cod sursa (job #3310745) | Monitorul de evaluare | Cod sursa (job #3355260)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define INF 4000001
int main() {
ifstream fin("distante.in");
ofstream fout("distante.out");
int n, m, start, t;
fin >> t;
for (int i = 1; i <= t; i++) {
fin >> n >> m >> start;
vector<int> ex(n+1);
for (int j = 1; j <= n; j++)
fin >> ex[j];
vector<int> dist(n+1, INF);
vector<vector<pair<int,int>>> adj(n+1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int j = 0; j < m; j++) {
int a,b,c;
fin >>a>>b>>c;
adj[a].push_back({c, b});
}
q.push({0, start});
dist[start] = 0;
while (!q.empty()) {
int nod = q.top().second;
int cost = q.top().first;
q.pop();
if (cost > dist[nod]) continue;
for (auto x : adj[nod]) {
int n2 = x.second;
int weight = x.first;
if (dist[nod] + weight < dist[n2]) {
dist[n2] = dist[nod] + weight;
q.push({dist[n2], n2});
}
}
}
int j;
for (j = 1; j <= n; j++)
if (dist[j] != ex[j]) {
fout << "NU" <<'\n';
j = n + 3;
}
if (j != n + 4)
fout << "DA"<<'\n';
}
return 0;
}