Pagini recente » Cod sursa (job #981380) | Cod sursa (job #3280247) | Cod sursa (job #1749480) | Cod sursa (job #1231504) | Cod sursa (job #555412)
Cod sursa(job #555412)
#include<fstream>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 50010
using namespace std;
const int nmax=5000;
vector< pair<int,int> > e[maxn];
bool test[maxn];
queue<int> Q;
int T,N,M,S;
long d[maxn],dp[maxn];
ifstream f("distante.in");
ofstream g("distante.out");
void bellman(int S){
for(int i=1;i<=N;i++)
d[i]=nmax;
memset(test,false,nmax);
d[S]=0;
test[S]=true;
Q.push(S);
while(!Q.empty()){
int nod=Q.front();
Q.pop();
test[nod]=false;
for(vector< pair<int,int> >::iterator it=e[nod].begin();it!=e[nod].end();++it){
if(d[it->first]>d[nod]+it->second){
d[it->first]=d[nod]+it->second;
if(!test[it->first])
Q.push(it->first),test[it->first]=true;
}
if(d[nod]>d[it->first]+it->second){
d[nod]=d[it->first]+it->second;
if(!test[nod])
Q.push(nod),test[nod]=true;
}
}
}
}
void citire(){
int x,y,c;
f>>N>>M>>S;
for(int i=1;i<=N;i++)
f>>dp[i];
for(int i=1;i<=M;i++){
f>>x>>y>>c;
e[x].push_back(make_pair(y,c)),e[y].push_back(make_pair(x,c));
}
}
int compara(){
for(int i=1;i<=N;i++)
if(d[i]!=dp[i])
return 0;
return 1;
}
void sterge(){
for(int i=1;i<=N;i++)
e[i].clear();
}
int main(){
f>>T;
for(int i=1;i<=T;i++){
citire();
bellman(S);
sterge();
if(dp[S]!=0)
g<<"NU"<<'\n';
else if(compara())
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
g.close();
return 0;
}