Pagini recente » Cod sursa (job #1593391) | Cod sursa (job #72449) | Cod sursa (job #1997064) | Cod sursa (job #2298197) | Cod sursa (job #1444258)
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <deque>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int NMax = 50005;
const int INF = 1e9;
int v[NMax], cost[NMax];
vector < pair < int, int > > G[NMax];
void solve(int node){
pair < int, int > now;
for(int i = 0; i < G[node].size(); i++){
now = G[node][i];
if(cost[node] + now.second < cost[now.first]){
cost[now.first] = cost[node] + now.second;
solve(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));
}
solve(s);
if(good(n)){
fout << "DA\n";
} else {
fout << "NU\n";
}
for(int i = 1; i <= n; i++){
G[i].clear();
}
}
return 0;
}