Pagini recente » Cod sursa (job #3144941) | Cod sursa (job #182665) | Cod sursa (job #1816701) | Cod sursa (job #2063894) | Cod sursa (job #1631294)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
ifstream is("distante.in");
ofstream os("distante.out");
struct Edge
{
int node, cost;
const bool operator < (const Edge other) const
{
return cost > other.cost;
}
};
int n, t, m, s;
vector<vector<pair<int, int> > > g;
vector<int> d;
vector<int> verif;
priority_queue<Edge> Q;
int main()
{
is >> t;
int citire1;
int v1, v2, c3;
int x;
bool ok = true;
for(int i = 1; i <= t; ++i)
{
is >> n >> m >> s;
g = vector<vector<pair < int, int> > >(n+1);
d = vector<int>(n+1, INF);
verif = vector<int>(n+1, 0);
// verif.resize(n+1);
//memset(verif, 0, sizeof verif)
for(int j = 1; j <= n; ++j)
{
is >> citire1;
verif[j] = citire1;
}
/*if(verif[s] != 0)
{
os << "NU" << '\n';
continue;
}
*/
d[s] = 0;
for(int k = 1; k <= m;++k)
{
is >> v1 >> v2 >> c3;
g[v1].push_back({v2, c3});
g[v2].push_back({v1, c3});
}
Q.push({s, d[s]});
while(!Q.empty())
{
x = Q.top().node;
Q.pop();
for(const auto & y: g[x])
{
if(d[y.first] > d[x] + y.second)
{
d[y.first] = d[x] + y.second;
Q.push({y.first, d[y.first]});
}
}
}
for(int j = 1; j <= n; ++j)
{
if(d[j] != verif[j])
{
ok = false;
}
}
if(ok)
os << "DA" << '\n';
else
os << "NU" << '\n';
g.clear();
verif.clear();
d.clear();
}
is.close();
os.close();
return 0;
}