Pagini recente » Cod sursa (job #2477414) | Cod sursa (job #57072) | Cod sursa (job #1277899) | Cod sursa (job #2496496) | Cod sursa (job #575917)
Cod sursa(job #575917)
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define f first
#define s second
#define mp make_pair
#define pb push_back
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
struct cmp{bool operator()(pair<int,int> x,pair<int,int> y){return x.second>y.second;}};
priority_queue <pii,vector<pii>,cmp> h;
bool dijkstra()
{
vector <pii> g[50001];
int n,m,s,x,y,cost,j,i,nod;
int c[50001];
int corect[50001];
in>>n>>m>>s;
for(i=1;i<=n;i++)
in>>c[i];
for(;m;m--)
{
in>>x>>y>>cost;
g[x].pb(mp(y,cost));
g[y].pb(mp(x,cost));
}
if(c[s])return 0;
fill(corect,corect+n+1,999999);
corect[s]=0;
h.push(mp(s,0));
while(!h.empty())
{
nod=(h.top()).f;
for(j=0;j<g[nod].size();j++)
if(corect[nod]+g[nod][j].s<corect[g[nod][j].f])
{
corect[g[nod][j].f]=corect[nod]+g[nod][j].s;
h.push(mp(g[nod][j].f,corect[g[nod][j].f]));
}
h.pop();
}
for(i=1;i<=n;i++)
if(c[i]!=corect[i])
return 0;
return 1;
}
int main()
{
int T;
in>>T;
for(int i=0;i<T;i++)
if(dijkstra())
out<<"DA\n";
else out<<"NU\n";
in.close();
out.close();
return 0;
}