Cod sursa(job #2791869)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 31 octombrie 2021 11:55:24
Problema Distante Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#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]) {
            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;
}