Pagini recente » Cod sursa (job #1805201) | Cod sursa (job #1714712) | Cod sursa (job #1353603) | Cod sursa (job #2572880) | Cod sursa (job #1444270)
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <deque>
#include <cstring>
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];
deque < int > stak;
bool viz[NMax];
void solve(){
int node;
pair < int, int > now;
while(!stak.empty()){
node = stak.front();
stak.pop_front();
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;
if(!viz[now.first]){
stak.push_back(now.first);
viz[now.first] = 1;
}
}
}
}
}
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;
memset(viz, 0, sizeof(viz));
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));
}
stak.push_back(s);
viz[s] = 1;
solve();
if(good(n)){
fout << "DA\n";
} else {
fout << "NU\n";
}
for(int i = 1; i <= n; i++){
G[i].clear();
}
}
return 0;
}