Pagini recente » Cod sursa (job #337616) | Cod sursa (job #1990512) | Cod sursa (job #328466) | Cod sursa (job #574911) | Cod sursa (job #2485307)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
struct abc{
int nod, cost;
bool operator <(const abc & a)const{
return cost>a.cost;
}
};
abc mp(int a, int b){
abc t;
t.nod=a;
t.cost=b;
return t;
}
vector <abc> v[100001];
priority_queue <abc> q;
int n, m, s, a, x, y, d[50005], dist[100001], ok, nr;
void dijkstra(int nod){
q.push({nod, 0});
memset(dist, inf, sizeof dist);
dist[nod]=0;
while(!q.empty()){
int var=q.top().nod;
//int var2=q.top().cost;
q.pop();
for(auto i:v[var]){
if(dist[i.nod]>i.cost + dist[var]){
dist[i.nod]=i.cost + dist[var];
q.push({i.nod, dist[i.nod]});
}
}
}
}
int main()
{
in>>nr;
abc b;
for(int i=1;i<=nr;i++){
in>>n>>m>>s;
for(int j=1;j<=n;j++){
in>>d[j];
}
for(int k=1;k<=m;k++){
in>>a>>x>>y;
v[a].push_back({x, y});
v[x].push_back({a, y});
}
dijkstra(s);
ok=0;
dist[s]=0;
//for(int k=1;k<=n;k++){
//cout<<dist[k]<<" ";
// }
//cout<<endl;
for(int k=1;k<=n;k++){
if(dist[k]!=d[k]){
ok=1;
cout<<"NU"<<endl;
break;
}
}
if(ok==0){
cout<<"DA"<<endl;
}
for(int j=1;j<=m;j++){
v[j].clear();
}
}
}