Cod sursa(job #1502173)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 14 octombrie 2015 12:00:44
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int Nmax = 50000;
const int Mmax = 100000;
const int NIL = -1;
const int INF = 2e9;

int D[Nmax];
int d[Nmax];

struct List {
    int v, cost;
    int next;
};

struct cmp {
    bool operator() (const int &A, const int &B) {
        return d[A] > d[B];
    }
};

priority_queue <int, vector<int>, cmp> heap;

List l[2 * Mmax];
int adj[Nmax];

void inline addEdge(int u, int v, int cost, int pos) {
    l[pos].v = v;
    l[pos].cost = cost;
    l[pos].next = adj[u];
    adj[u] = pos;
}

int main(void) {
    freopen("distante.in", "r", stdin);
    freopen("distante.out", "w", stdout);
    int T, n, m, source;
    int u, v, cost;

    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &n, &m, &source);

        source--;

        for (int i = 0; i < n; i++) {
            scanf("%d", &D[i]);
            adj[i] = NIL;
            d[i] = INF;
        }
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &u, &v, &cost);
            addEdge(u - 1, v - 1, cost, 2 * i);
            addEdge(v - 1, u - 1, cost, 2 * i + 1);
        }
        d[source] = 0;
        heap.push(source);
        do {
            u = heap.top();
            heap.pop();
            for (int i = adj[u]; i != NIL; i = l[i].next) {
                v = l[i].v;
                cost = d[u] + l[i].cost;
                if (d[v] > cost) {
                    d[v] = cost;
                    heap.push(v);
                }
            }
        } while (!heap.empty());
        u = 0;
        while (u < n && d[u] == D[u]) {
            u++;
        }
        puts(u == n ? "DA" : "NU");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}