Cod sursa(job #2797033)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 9 noiembrie 2021 10:29:35
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.3 kb
#include <queue>
#include <fstream>

using namespace std;

#define N_MAX 50000
#define INF (1 << 30)
#define BUFF_SIZE (1 << 14)

priority_queue<pair<int, int>> dijkstra;
vector<pair<int, int>> G[N_MAX];
int ans[N_MAX], expected_ans[N_MAX];

FILE *_fin;
int _in_loc;
char _in_buff[BUFF_SIZE];


void read_init(const char* nume) {
    _fin = fopen(nume, "r");
    _in_loc = BUFF_SIZE - 1;
}

char read_ch() {
    ++_in_loc;
    if (_in_loc == BUFF_SIZE) {
        _in_loc = 0;
        fread(_in_buff, 1, BUFF_SIZE, _fin);
    }
    return _in_buff[_in_loc];
}

int read_u32() {
    int u32 = 0;
    char c;
    while (!isdigit(c = read_ch()) && c != '-');
    int sgn = 1;
    if (c == '-') {
        sgn = -1;
    } else {
        u32 = c - '0';
    }
    while (isdigit(c = read_ch())) {
        u32 = u32 * 10 + c - '0';
    }
    return u32 * sgn;
}



void DIJKSTRA() {
    while ( !dijkstra.empty() ) {
        pair<int, int> elem = dijkstra.top();
        dijkstra.pop();
        if ( ans[elem.second] == -INF || ans[elem.second] == elem.first ) {
            for ( pair<int, int> copil : G[elem.second] ) {
                if ( ans[copil.first] < elem.first + copil.second ) {
                    ans[copil.first] = elem.first + copil.second;
                    dijkstra.push( {ans[copil.first], copil.first} );
                }
            }
        }
    }
}

ofstream cout ( "distante.out" );

int main() {
    int t, i, j, n, m, s, a, b, cost;
    read_init( "distante.in" );
    t = read_u32();
    ios::sync_with_stdio(true);
    cout.tie(0);
    for ( i = 0; i < t; i++ ) {
        n = read_u32();
        m = read_u32();
        s = read_u32();
        for ( j = 0; j <= n; j++ )
            ans[j] = -INF;
        ans[s] = -1;
        for ( j = 1; j <= n; j++ ) {
            expected_ans[j] = read_u32();
            G[j].clear();
        }
        for ( j = 1; j <= m; j++ ) {
            a = read_u32();
            b = read_u32();
            cost = read_u32();
            G[a].emplace_back(b, cost * -1);
            G[b].emplace_back(a, cost * -1);
        }
        dijkstra.push({-1, s});
        DIJKSTRA();
        j = 1;
        while ( j <= n && ( ans[j] + 1 ) * -1 == expected_ans[j] ) {
            j++;
        }
        if ( j <= n )
            cout << "NU\n";
        else
            cout << "DA\n";
    }
    return 0;
}