Cod sursa(job #2711675)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 24 februarie 2021 16:28:19
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <bits/stdc++.h>

using namespace std;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

InParser fin("distante.in");
ofstream fout("distante.out");

const int NMAX = 5e4 + 4;
const int INF = 2e9;
int dist[NMAX];

void test_case() {
    int N, M, source;
    fin >> N >> M >> source;
    vector<pair<int,int>> G[N + 1];
    for(int i = 1; i <= N; ++i)
        fin >> dist[i];
    for(int i = 0; i < M; ++i) {
        int u, v, w;
        fin >> u >> v >> w;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
    }
    vector<int> dp(N + 1, INF);
    dp[source] = 0;
    priority_queue<pair<int,int>> Q;
    Q.emplace(0, source);
    while(!Q.empty()) {
        int u = Q.top().second,
            d = -Q.top().first;
        Q.pop();
        if(d != dp[u])
            continue;
        for(const auto &v : G[u])
            if(dp[v.first] > dp[u] + v.second) {
                dp[v.first] = dp[u] + v.second;
                Q.emplace(-dp[v.first], v.first);
            }
    }
    for(int i = 1; i <= N; ++i)
        if(dist[i] != dp[i]) {
            fout << "NU\n";
            return;
        }
    fout << "DA\n";
}

int main() {
    int T;
    fin >> T;
    for(int tc = 0; tc < T; ++tc)
        test_case();
}