Pagini recente » Cod sursa (job #2841778) | Cod sursa (job #1949109) | Cod sursa (job #2184385) | Cod sursa (job #739119) | Cod sursa (job #2711675)
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
InParser fin("distante.in");
ofstream fout("distante.out");
const int NMAX = 5e4 + 4;
const int INF = 2e9;
int dist[NMAX];
void test_case() {
int N, M, source;
fin >> N >> M >> source;
vector<pair<int,int>> G[N + 1];
for(int i = 1; i <= N; ++i)
fin >> dist[i];
for(int i = 0; i < M; ++i) {
int u, v, w;
fin >> u >> v >> w;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
vector<int> dp(N + 1, INF);
dp[source] = 0;
priority_queue<pair<int,int>> Q;
Q.emplace(0, source);
while(!Q.empty()) {
int u = Q.top().second,
d = -Q.top().first;
Q.pop();
if(d != dp[u])
continue;
for(const auto &v : G[u])
if(dp[v.first] > dp[u] + v.second) {
dp[v.first] = dp[u] + v.second;
Q.emplace(-dp[v.first], v.first);
}
}
for(int i = 1; i <= N; ++i)
if(dist[i] != dp[i]) {
fout << "NU\n";
return;
}
fout << "DA\n";
}
int main() {
int T;
fin >> T;
for(int tc = 0; tc < T; ++tc)
test_case();
}