Pagini recente » Cod sursa (job #125416) | Cod sursa (job #3281814) | Profil mario_deaconescu | Cod sursa (job #3292443) | Cod sursa (job #1886576)
#include <algorithm>
#include <fstream>
#include <cstring>
#include <cassert>
#include <vector>
#include <set>
using namespace std;
const int NMAX = 50005;
const int INF = 0x3f3f3f3f;
vector<pair<int,int> > G[NMAX];
int dist[NMAX];
int dis[NMAX];
ifstream f("distante.in");
ofstream g("distante.out");
int main()
{ int t;
int n, m,s;
vector<pair<int,int> >::iterator it;
f>>t;
for(int k=1;k<=t;k++)
{f>>n>>m>>s;
for(int i=1;i<=n;i++)
f>>dis[i];
for (int i=1;i<=m;++i)
{int from,to,cost;
f>>from>>to>>cost;
G[from].push_back(make_pair(to, cost));
}
memset(dist,INF,sizeof dist);
dist[s]=0;
set<pair<int,int> > h;
h.insert(make_pair(0,s));
while (!h.empty())
{int node=h.begin()->second;
int d=h.begin()->first;
h.erase(h.begin());
for (it=G[node].begin();it!=G[node].end();++it)
{int to=it->first;
int cost=it->second;
if (dist[to]>dist[node]+cost)
{if (dist[to]!=INF) h.erase(h.find(make_pair(dist[to], to)));
dist[to]=dist[node]+cost;
h.insert(make_pair(dist[to],to));
}
}
}
bool ok=1;
for (int i=1;i<=n;++i)
{if (dist[i]==INF) dist[i]=0;
if(dist[i]!=dis[i]){ok=0;break;}
}
if(ok==1)g<<"DA";
else g<<"NU";
g<< '\n';
}
}