Pagini recente » Cod sursa (job #2476841) | Cod sursa (job #268053) | Cod sursa (job #1385659) | Cod sursa (job #1754912) | Cod sursa (job #1393155)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int INF = 0x3f3f3f3f;
int T, N, M, S;
int Dinit[50005], Dmin[50005];
vector<pair<int, int> > V[50005];
queue<int> Q;
bool inQ[50005], ok;
void bellman(int nod)
{
for (int i = 1; i <= N; ++i)
Dmin[i] = INF;
Dmin[nod] = 0;
Q.push(S);
inQ[S] = true;
while (!Q.empty())
{
int now = Q.front();
Q.pop();
inQ[now] = false;
for (vector<pair<int, int> >::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (Dmin[now] + it->second < Dmin[it->first])
{
Dmin[it->first] = Dmin[now] + it->second;
if (Dmin[it->first] < Dinit[it->first])
ok = false;
if (!inQ[it->first])
{
Q.push(it->first);
inQ[it->first] = true;
}
}
}
}
}
int main()
{
fin >> T;
while (T--)
{
fin >> N >> M >> S;
for (int i = 1; i <= N; ++i)
V[i].clear();
for (int i = 1; i <= N; ++i)
fin >> Dinit[i];
for (int i = 1, nod1, nod2, cost; i <= M; ++i)
{
fin >> nod1 >> nod2 >> cost;
V[nod1].push_back(make_pair(nod2, cost));
V[nod2].push_back(make_pair(nod1, cost));
}
ok = true;
bellman(S);
if (ok)
fout << "DA\n";
else
fout << "NU\n";
}
fin.close();
fout.close();
return 0;
}