Pagini recente » Cod sursa (job #339663) | Cod sursa (job #3125401) | Cod sursa (job #324281) | Cod sursa (job #625401) | Cod sursa (job #2536013)
#include <iostream>
#include <fstream>
#include <queue>
#define inf 2e9
using namespace std;
ifstream in ("distante.in");
ofstream out ("distante.out");
const int N=50001;
const int M=100001;
int vf[M],urm[M],lst[N],cost[M],nr,n,m,d[N],b[N];
priority_queue < pair<int,int> > h;
bool sel[N],fl;
void adauga (int x, int y, int c)
{
vf[++nr]=y;
urm[nr]=lst[x];
cost[nr]=c;
lst[x]=nr;
}
void dijkstra (int x0)
{
for (int i=1;i<=n;i++)
{
d[i]=inf;
sel[i]=false;
//lst[i]=0;
}
d[x0]=0;
h.push(make_pair(0,x0));
while (!h.empty() && fl==true)
{
while (!h.empty() && sel[h.top().second])
{
h.pop();
}
if (h.empty()) return;
int x=h.top().second;
sel[x]=true;
for (int p=lst[x];p!=0;p=urm[p])
{
int y=vf[p];
int c=cost[p];
if (d[x]+c<d[y])
{
d[y]=d[x]+c;
if (d[y]<b[y]) fl=false;
h.push(make_pair(-d[y],y));
}
}
}
}
int main()
{
int t,x,y,c,s;
in>>t;
while (t--)
{
nr=0;
in>>n>>m>>s;
for (int i=1;i<=n;i++)
{in>>b[i];lst[i]=0;}
for (int i=1;i<=m;i++)
{
in>>x>>y>>c;
adauga (x,y,c);
}
fl=true;
dijkstra (s);
if (fl==false) out<<"NU"<<'\n';
else out<<"DA"<<'\n';
/*bool t=true;
for (int i=1;i<=n;i++)
{
if (d[i]==inf) d[i]=0;
if (d[i]!=b[i])
{
t=false;
//cout<<i<<' '<<d[i]<<' ';
out<<"NU"<<'\n';
break;
}
}
if (t==true) out<<"DA"<<'\n';*/
}
return 0;
}