Pagini recente » Cod sursa (job #2205624) | Cod sursa (job #24448) | Cod sursa (job #1849330) | Cod sursa (job #2738702) | Cod sursa (job #1144762)
#include <fstream>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
const int Nmax = 50005;
const int Inf = 0x3f3f3f3f;
typedef pair<int, int> pii;
vector<pii> G[Nmax];
int dist[Nmax];
int main()
{
ifstream f ("distante.in");
ofstream g ("distante.out");
int T, N, M, s, a, b, w;
f >> T;
for (int t = 0; t < T; t++) {
f >> N >> M >> s;
s--;
for (int i = 0; i < N; i++) {
f >> dist[i];
G[i].clear();
}
G[s].push_back(make_pair(s, 0));
for (int e = 0; e < M; e++) {
f >> a >> b >> w;
G[a-1].push_back(make_pair(b-1, w));
G[b-1].push_back(make_pair(a-1, w));
}
bool ok = (dist[s] == 0);
for (int i = 0; i < N && ok; i++) {
int good = false;
for (auto x: G[i]) {
if (dist[x.first] > dist[i] + x.second) {
ok = false;
}
if (dist[i] == dist[x.first] + x.second)
good = true;
}
if (!good) ok = false;
}
if (ok) g << "DA\n";
else g << "NU\n";
}
return 0;
}