Cod sursa(job #2507073)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 9 decembrie 2019 16:30:11
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");
const int MAXN = 50010, INF = 0x3f3f3f3f;

struct Node {
    int node, cost;
    bool operator<(const Node& other) const {
        if (cost != other.cost)
            return cost < other.cost;
        return node < other.node;
    }
};

int dist[MAXN], res[MAXN], n, m, s, t;
vector<Node> edges[MAXN];
set<Node> heap;

void reset() {
    for (int i = 1; i <= n; ++i)
        dist[i] = INF;
    for (int i = 1; i <= n; ++i)
        edges[i].clear();
}

void dijkstra(int source) {
    dist[source] = 0;
    heap.insert({source, 0});
    while(!heap.empty()) {
        int node = heap.begin()->node;
        heap.erase(heap.begin());
        for (const auto& it: edges[node])
            if (dist[node] + it.cost < dist[it.node]) {
                if (dist[it.node] != INF)
                    heap.erase({it.node, dist[it.node]});
                dist[it.node] = dist[node] + it.cost;
                heap.insert({it.node, dist[it.node]});
            }
    }
}

bool verify() {
    for (int i = 1; i <= n; ++i)
        if (dist[i] != res[i])
            return false;
    return true;
}

void read() {
    fin >> n >> m >> s;
    for (int i = 0; i < m; ++i) {
        int x, y, cost;
        fin >> x >> y >> cost;
        edges[x].push_back({y, cost});
        edges[y].push_back({x, cost});
    }
}

int main() {
    fin >> t;
    for (int i = 0; i < t; ++i) {
        read();
        dijkstra(s);
        if (verify())
            fout << "DA\n";
        else
            fout << "NU\n";
        reset();
    }
    return 0;
}