Cod sursa(job #2797030)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 9 noiembrie 2021 10:23:04
Problema Distante Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <queue>
#include <fstream>

using namespace std;

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

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

void DIJKSTRA() {
    while ( !dijkstra.empty() ) {
        pair<int, int> elem = dijkstra.top();
        dijkstra.pop();
        if ( ans[elem.second] == -INF || ans[elem.second] == elem.first ) {
            for ( auto 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} );
                }
            }
        }
    }
}

ifstream cin ( "distante.in" );
ofstream cout ( "distante.out" );

int main() {
    int t, i, j, n, m, s, a, b, cost;
    cin >> t;
    ios::sync_with_stdio(true);
    cin.tie(0);
    cout.tie(0);
    for ( i = 0; i < t; i++ ) {
        cin >> n >> m >> s;
        for ( j = 0; j <= n; j++ )
            ans[j] = -INF;
        ans[s] = -1;
        for ( j = 1; j <= n; j++ ) {
            cin >> expected_ans[j];
            G[j].clear();
        }
        for ( j = 1; j <= m; j++ ) {
            cin >> a >> b >> cost;
            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;
}