Pagini recente » Cod sursa (job #28749) | Cod sursa (job #3256719) | Cod sursa (job #2524145) | Cod sursa (job #3267545) | Cod sursa (job #2791876)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
fstream f("distante.in", ios::in);
fstream g("distante.out", ios::out);
const int NMAX = 50001;
struct Edge {
int node, cost;
bool operator <(const Edge edge) const {
return cost > edge.cost;
}
};
vector<Edge> edges[NMAX];
priority_queue<Edge> bestNext;
int supposedDistance[NMAX];
int actualDistance[NMAX];
const int INFINITY = 100000001;
void computeActualDistances(int source) {
bestNext.push({source, 0});
while(!bestNext.empty()) {
int cost = bestNext.top().cost;
int node = bestNext.top().node;
bestNext.pop();
if(actualDistance[node] == INFINITY) {
actualDistance[node] = cost;
}
else {
continue;
}
for(auto & edge : edges[node]) {
if(actualDistance[edge.node] == INFINITY) {
bestNext.push({edge.node, edge.cost + cost});
}
}
}
}
void solve() {
int n, m, source;
f >> n >> m >> source;
for(int i = 1; i <= n; ++i) {
f >> supposedDistance[i];
actualDistance[i] = INFINITY;
}
for(int i = 0; i < m; ++i) {
int a, b, c;
f >> a >> b >> c;
edges[a].push_back({b, c});
edges[b].push_back({a, c});
}
computeActualDistances(source);
for(int i = 1; i <= n; ++i) {
edges[i].clear();
}
for(int i = 1; i <= n; ++i) {
if(supposedDistance[i] != actualDistance[i]) {
g << "NU\n";
return;
}
}
g << "DA\n";
}
int main()
{
int t;
f >> t;
for(int i = 0; i < t; ++i)
{
solve();
}
return 0;
}