Pagini recente » Cod sursa (job #1395365) | Cod sursa (job #1394778) | Cod sursa (job #2507605) | Cod sursa (job #440575) | Cod sursa (job #1478609)
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <deque>
#include <set>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int NMax = 50005;
const int INF = 1e9;
bool good;
int v[NMax], cost[NMax];
vector < pair < int, int > > G[NMax];
set < pair < int, int > > T;
void solve(int start){
int node, val;
pair < int, int > now;
T.insert(make_pair(0, start));
while(!T.empty()){
node = (*T.begin()).second;
val = (*T.begin()).first;
if(val != v[node]){
good = false;
return;
}
T.erase(*T.begin());
for(int i = 0; i < G[node].size(); i++){
now = G[node][i];
if(cost[now.first] > cost[node] + now.second){
cost[now.first] = cost[node] + now.second;
T.insert(make_pair(cost[now.first], now.first));
}
}
}
}
/*inline bool good(const int &x){
for(int i = 1; i <= x; i++){
if(cost[i] != v[i]){
return 0;
}
}
return 1;
}*/
int main()
{
int t, n, m, s, a, b, c;
fin >> t;
while(t--){
fin >> n >> m >> s;
for(int i = 1; i <= n; i++){
fin >> v[i];
cost[i] = INF;
}
cost[s] = 0;
for(int i = 1; i <= m; i++){
fin >> a >> b >> c;
G[a].push_back(make_pair(b, c));
G[b].push_back(make_pair(a, c));
}
good = true;
solve(s);
if(good){
fout << "DA\n";
} else {
fout << "NU\n";
}
for(int i = 1; i <= n; i++){
G[i].clear();
}
}
return 0;
}