Pagini recente » Cod sursa (job #1370407) | Cod sursa (job #1086781) | Cod sursa (job #1478665) | Monitorul de evaluare | Cod sursa (job #2498686)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin ("distante.in");
ofstream fout ("distante.out");
struct graf {
int nod, cost;
bool operator < (const graf & other) const {
return cost > other.cost;
}
};
vector <graf> g[50005];
priority_queue <graf> q;
int dist[50005], v[50005];
void resetGraph (int n)
{
for (int i = 1; i <= n; i ++) g[i].clear();
}
void dijkstra (int start)
{
memset(v, INF, sizeof v);
v[start] = 0;
q.push({start, 0});
while (!q.empty()) {
int cost = q.top().cost;
int nod = q.top().nod;
q.pop();
if (v[nod] != cost) continue;
int n = g[nod].size();
for (int i = 0; i < n; i ++) {
int vf = g[nod][i].nod;
if (v[vf] > cost + g[nod][i].cost) v[vf] = cost + g[nod][i].cost, q.push({vf, v[vf]});
}
}
}
bool verif (int n)
{
for (int i = 1; i <= n; i ++) {
if (v[i] != dist[i]) return false;
}
return true;
}
int main()
{
int t;
fin >> t;
for (int j = 1; j <= t; j ++) {
int n, m, s, a, b, c;
fin >> n >> m >> s;
for (int i = 1; i <= n; i ++) fin >> dist[i];
for (int i = 1; i <= m; i ++) {
fin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
dijkstra(s);
if (verif(n)) fout << "DA\n";
else fout << "NU\n";
resetGraph(n);
}
fin.close();
fout.close();
return 0;
}