Pagini recente » Cod sursa (job #2260795) | Cod sursa (job #1108094) | Cod sursa (job #1672867) | Cod sursa (job #3160548) | Cod sursa (job #2923353)
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
struct Muchie
{
int vf, cost;
};
vector <pair<int,int>> graf[50001];
struct comparare
{
bool operator () (Muchie & a, Muchie & b)
{
return a.cost > b.cost;
}
};
priority_queue < Muchie,vector<Muchie>, comparare> h;
const int MAX=1000000100;
int v[50001],dist[50001];
bool inq[50001];
int n;
void bellman(int s)
{
Muchie M;
int x,y,cost;
h.push({s,0});
dist[s]=0;
inq[s]=1;
while(h.empty()==false)
{
M=h.top();
if(M.cost>v[M.vf])
continue;
if(M.cost<v[M.vf])
{
printf("NU \n");
while(h.empty()==false)
{
h.pop();
}
return;
}
inq[M.vf]=0;
h.pop();
for(unsigned int i=0; i<graf[M.vf].size(); i++)
{
y=graf[M.vf][i].first;
cost=graf[M.vf][i].second;
if(dist[y]>dist[M.vf]+cost)
{
dist[y]=dist[M.vf]+cost;
if(inq[y]==0)
{
h.push({y,dist[y]});
inq[y]=1;
}
}
}
}
bool ok=1;
for(int i=1; i<=n && ok==1; i++)
{
if(dist[i]!=v[i])
ok=0;
}
if(ok==0)
printf("NU \n");
else
printf("DA \n");
}
int main()
{
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
int T,m,s,x,y,cost;
scanf("%d",&T);
int l;
int i;
for( l=1; l<=T; l++)
{
scanf("%d%d%d",&n,&m,&s);
for( i=1; i<=n; i++)
{
scanf("%d",&v[i]);
dist[i]=MAX;
graf[i].clear();
}
for( i=1; i<=m; i++)
{
scanf("%d%d%d",&x,&y,&cost);
graf[x].push_back(make_pair(y,cost));
graf[y].push_back(make_pair(x,cost));
}
bellman(s);
}
return 0;
}