Pagini recente » Istoria paginii runda/preoji_4/clasament | Istoria paginii runda/tarni_und_veli | Cod sursa (job #775863) | preoji/clasament/9 | Cod sursa (job #2524834)
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "distante";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
int n,m,s;
int target[50005];
int d[50005];
bool vis[50005];
int a,b,c;
struct Edge {
int destination;
int cost;
};
void solve() {
fin >> n >> m >> s;
s--;
for (int i = 0; i < n; ++i) {
fin >> target[i];
}
vector<Edge> g[50005];
for (int i = 0; i < n; ++i) {
fin >> a >> b >> c;
g[a - 1].push_back({b - 1,c});
}
bool ok = true;
queue<int> q;
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
d[s] = 0;
q.push(s);
vis[s] = true;
while (!q.empty()) {
auto node = q.front();
q.pop();
vis[node] = false;
for (auto x : g[node]) {
if (x.cost + d[node] < d[x.destination]) {
d[x.destination] = x.cost + d[node];
q.push(x.destination);
vis[x.destination] = true;
}
}
}
for (int i = 0 ; i < n ; ++i) {
if (d[i] != target[i]) {
ok = false;
break;
}
}
fout << (ok ? "DA\n" : "NU\n");
}
int main() {
int t;
fin >> t;
while (t) {
t--;
solve();
}
return 0;
}