Pagini recente » Cod sursa (job #1409017) | Cod sursa (job #1165623) | Cod sursa (job #2748850) | Cod sursa (job #2381159) | Cod sursa (job #2532146)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
#define INF (1 << 30)
#define NMAX 50005
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
set < pair <int,int> > st;
set < pair <int,int> > :: iterator it;
vector < pair <int , int> > G[NMAX];
vector <int> check(NMAX);
int dist[NMAX];
int T, N, M, S;
bool checkCondition()
{
for(int i = 1; i <= N; i++){
if(dist[i] != check[i])
return false;
}
return true;
}
void shortPath(int start)
{
int currNode, g;
dist[start] = 0;
st.insert(make_pair(0,start));
while(!st.empty()){
it = st.begin();
st.erase(st.begin());
currNode = (*it).second;
g = (*it).first;
if(g > dist[currNode]){
continue;
}
for(int i = 0; i < G[currNode].size(); i++){
if(dist[G[currNode][i].second] > dist[currNode] + G[currNode][i].first){
dist[G[currNode][i].second] = dist[currNode] + G[currNode][i].first;
st.insert(make_pair(dist[G[currNode][i].second],G[currNode][i].second));
}
}
}
}
int main()
{
int x, y, c;
fin >> T;
for(int t = 1; t <= T; t++){
fin >> N >> M >> S;
for(int i = 1; i <= N; i++){
fin >> x;
dist[i] = INF;
check[i] = x;
}
for(int i = 1; i <= M; i++){
fin >> x >> y >> c;
G[x].push_back(make_pair(c,y));
G[y].push_back(make_pair(c,x));
}
shortPath(S);
if(checkCondition()){
fout << "DA" << "\n";
}
else{
fout << "NU" << "\n";
}
}
return 0;
}