Cod sursa(job #2796981)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 9 noiembrie 2021 08:59:29
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <stdio.h>
#include <queue>
#include <vector>

#define MAX_N 50000

using namespace std;

struct muchie {
    int nod, cost;
    bool operator < (const muchie &aux) const {
        return cost > aux.cost;
    }
};

int dist[MAX_N], distCer[MAX_N];
vector <muchie> muchii[MAX_N];
priority_queue <muchie> pq;

int main() {
    FILE *fin, *fout;
    int t, n, m, s, x, y, c, i;
    struct muchie crt, next;

    fin = fopen( "distante.in", "r" );
    fout = fopen( "distante.out", "w" );

    fscanf( fin, "%d", &t );
    while ( t-- ) {
        fscanf( fin, "%d%d%d", &n, &m, &s );
        s--;
        for ( i = 0; i < n; i++ ) {
            fscanf( fin, "%d", &distCer[i] );
            dist[i] = -1;
            muchii[x].clear();
        }
        for ( i = 0; i < m; i++ ) {
            fscanf( fin, "%d%d%d", &x, &y, &c );
            x--;
            y--;
            muchii[x].push_back( { y, c } );
            muchii[y].push_back( { x, c } );
        }

        pq.push( { s, 0 } );
        while ( !pq.empty() ) {
            crt = pq.top();
            pq.pop();
            if ( dist[crt.nod] == -1 ) {
                dist[crt.nod] = crt.cost;
                for ( i = 0; i < muchii[crt.nod].size(); i++ ) {
                    next = muchii[crt.nod][i];
                    if ( dist[next.nod] == -1 )
                        pq.push( { next.nod, dist[crt.nod] + next.cost } );
                }
            }
        }

        i = 0;
        while ( i < n && dist[i] == distCer[i] )
            i++;

        if ( i == n )
            fprintf( fout, "DA\n" );
        else
            fprintf( fout, "NU\n" );
    }

    fclose( fin );
    fclose( fout );

    return 0;
}