Pagini recente » Cod sursa (job #3164148) | Cod sursa (job #1378508) | Cod sursa (job #2583217) | Cod sursa (job #3148164) | Cod sursa (job #1028691)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int N = 100001;
const int C = 1001;
const int INF = N * C ;
struct muchie{int vecin,cost;};
muchie makeMuchie(int a,int b){muchie x;x.vecin=a;x.cost=b;return x;}
queue<int> coada;
vector<muchie> muchii[N];
int cost_cerut[N];
int cost[N];
int n , m , nrteste , sursa;
bool bellman(){
while(!coada.empty()){
int old_varf = coada.front();
int old_cost = cost[old_varf];
coada.pop();
for(int i=0;i<muchii[old_varf].size();i++){
int new_varf = muchii[old_varf][i].vecin;
int new_cost = muchii[old_varf][i].cost;
if(cost[new_varf] > new_cost + old_cost){
coada.push(new_varf);
cost[new_varf] = new_cost + old_cost;
}
}
}
for(int i=1;i<=n;i++){
if(cost_cerut[i]!=cost[i]){
return false;
}
}
return true;
}
void calctest(){
in>>n>>m>>sursa;
for(int i=1;i<=n;i++){
in>>cost_cerut[i];
cost[i]=INF;
muchii[i].clear();
}
for(int i=1;i<=m;i++){
int x,y,z;
in>>x>>y>>z;
muchii[x].push_back(makeMuchie(y,z));
muchii[y].push_back(makeMuchie(x,z));
}
coada.push(sursa);
cost[sursa]=0;
out<< (bellman()==true ? "DA" : "NU")<<"\n";
}
int main()
{
in>>nrteste;
for(int i=1;i<=nrteste;i++){
calctest();
}
return 0;
}