Pagini recente » Utilizatori inregistrati la FMI No Stress 3 | Cod sursa (job #1587744) | Cod sursa (job #269300) | Cod sursa (job #1402669) | Cod sursa (job #2487072)
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cstring>
using namespace std;
//#include <iostream>
#include <fstream>
//ifstream cin ("input.in");
//ofstream cout ("output.out");
ifstream cin ("distante.in");
ofstream cout ("distante.out");
static const int INF = 1e9;
static const int NMAX = 5e4+5;
class cmp {
public:
const bool operator () ( const pair<int, int> &a, const pair<int, int> &b ) {
return a.second > b.second;
}
};
void Dijkstra(vector <int> &v, vector<pair<int, int>> graph[NMAX], int s) {
priority_queue <pair<int, int>, vector<pair<int, int>>, cmp> pq;
pq.push({s, v[s]});
while (pq.size()) {
int vertex = pq.top().first;
int dist = pq.top().second;
pq.pop();
if ( v[vertex] != dist ) {
continue;
}
for ( auto x:graph[vertex] ) {
if ( v[x.first] > v[vertex]+x.second ) {
v[x.first] = v[vertex]+x.second;
pq.push({x.first, v[x.first]});
}
}
}
}
string Solve (int n, int m, int s) {
vector <int> v(n+1, 0);
vector <pair<int, int>> graph[NMAX];
for ( int i = 1; i <= n; ++i ) {
cin>>v[i];
}
for ( int i = 1; i <= m; ++i ) {
int a, b, c;
cin>>a>>b>>c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
if ( v[s] ) {
return "NU";
}
vector <int> corV(n+1, INF);
corV[s] = 0;
Dijkstra(corV, graph, s);
/*(for ( int i = 1; i <= n; ++i ) {
cout<<corV[i]<<" "<<v[i]<<'\n';
}*/
for ( int i = 1; i <= n; ++i ) {
if ( v[i] != corV[i] ) {
return "NU";
}
}
return "DA";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin>>t;
while ( t-- ) {
int n, m, s;
cin>>n>>m>>s;
cout<<Solve(n, m, s)<<'\n';
}
}