Cod sursa(job #2147269)

Utilizator LittleWhoFeraru Mihail LittleWho Data 28 februarie 2018 16:45:57
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 50001;
const int INF = 1 << 30;

struct edge {
    int to, cost;

    bool operator() (const edge& lhs, const edge& rhs) {
        return lhs.cost > rhs.cost;
    }
};

vector<edge> graph[nmax];
bitset<nmax> visited;
int dist[nmax];
int dist_bronzarel[nmax];
bool ok;

int t, n, m, src;

inline void reinit() {
    for (int i = 1; i <= n; i++) {
        graph[i].clear();
        dist[i] = INF;
        visited[i] = false;
    }
    ok = true;
}

inline void dijkstra() {
    priority_queue<edge, vector<edge>, edge> pq;
    dist[src] = 0;
    pq.push({src, 0});

    int node, w;
    while (!pq.empty()) {
        node = pq.top().to;
        w = pq.top().cost;
        pq.pop();

        if (visited[node]) {
            continue;
        }
        visited[node] = true;

        for (auto &next_edge: graph[node]) {
            if (dist[next_edge.to] > w + next_edge.cost) {
                dist[next_edge.to] = w + next_edge.cost;
                pq.push({next_edge.to, dist[next_edge.to]});
            }
        }
    }
}

int main() {
    freopen("carici.in", "r", stdin);

    freopen("distante.in", "r", stdin);
    freopen("distante.out", "w", stdout);

    scanf("%d", &t);

    while (t--) {
        scanf("%d%d%d", &n, &m, &src);

        for (int i = 1; i <= n; i++) {
            scanf("%d", &dist_bronzarel[i]);
        }

        reinit();

        for (int i = 0, x, y, c; i < m; i++) {
            scanf("%d%d%d", &x, &y, &c);
            graph[x].push_back({y, c});
            graph[y].push_back({x, c});
        }

        dijkstra();
        for (int i = 1; i <= n; i++) {
            if (dist[i] != dist_bronzarel[i]) {
                ok = false;
                break;
            }
        }

        if (ok) {
            cout << "DA\n";
        } else {
            cout << "NU\n";
        }
    }

    return 0;
}