Pagini recente » Cod sursa (job #2150805) | Cod sursa (job #2724698) | Cod sursa (job #941887) | Cod sursa (job #2069216) | Cod sursa (job #2959804)
#include <fstream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int Inf = 0x3f3f3f3f;
using VI = vector<int>;
using PI = pair<int, int>;
using VP = vector<PI>;
using VVP = vector<VP>;
int T, N, M, Sursa;
VVP G; // graful citit
VI D, D_2;
void Read();
void Dijkstra(int x, VI& D);
int main(){
Read();
return 0;
}
void Dijkstra(int x, VI& D){
priority_queue<PI, vector<PI>, greater<PI>> Q;
D[x] = 0;
Q.emplace(0, x);
int dx, y, w;
while (!Q.empty()){
tie(dx, x) = Q.top();
Q.pop();
if (dx > D[x]) continue;
for (auto pair : G[x]){
tie (y, w) = pair;
if (D[y] > D[x] + w){
D[y] = D[x] + w;
Q.emplace(D[y], y);
}
}
}
}
void Read(){
fin >> T;
while (T--){
fin >> N >> M >> Sursa;
G = VVP(N + 1);
D = VI(N + 1, Inf);
D_2 = VI(N + 1);
for (int i = 1; i <= N; ++i)
fin >> D_2[i];
int x, y, c;
while (M--){
fin >> x >> y >> c;
G[x].emplace_back(y, c);
G[y].emplace_back(x, c);
}
Dijkstra(Sursa, D);
bool answer = true;
for (int i = 1; i <= N; ++i)
if (D[i] != D_2[i])
answer = false;
if (answer)
fout << "DA\n";
else
fout << "NU\n";
G.clear();
D.clear();
D_2.clear();
}
}