Cod sursa(job #2796989)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 9 noiembrie 2021 09:03:46
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.02 kb
#include <stdio.h>
#include <queue>
#include <vector>
#include <ctype.h>

#define MAX_N 50000

using namespace std;

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

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

int getNum() {
    char ch;
    int n;

    ch = getc( fin );
    while ( isspace( ch ) && ch != EOF )
        ch = getc( fin );

    n = 0;
    while ( isdigit( ch ) ) {
        n = n * 10 + ch - '0';
        ch = getc( fin );
    }

    return n;
}

int main() {
    FILE *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-- ) {
        n = getNum();
        m = getNum();
        s = getNum() - 1;
        for ( i = 0; i < n; i++ ) {
            distCer[i] = getNum();
            dist[i] = -1;
            muchii[i].clear();
        }
        for ( i = 0; i < m; i++ ) {
            x = getNum() - 1;
            y = getNum() - 1;
            c = getNum();
            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;
}