Cod sursa(job #2425064)

Utilizator malina99oanea ana malina malina99 Data 24 mai 2019 10:38:45
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#include <queue>

using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");
#define nmax 50000

void print_graph( vector <pair <int, int > > graph[nmax], int n ) {
    for( int i =1; i <= n; i++ ) {
        int lim = graph[i].size();
        for( int j = 0; j < lim; j++ ) {
            cout << graph[i][j].first << endl;
        }
    }
}

void distanteDij( vector < pair<int , int> > graph[nmax], vector <int > & dis, int n, int start ) {
    dis[start] = 0;
    priority_queue < pair <int, int> > heap;
    heap.push( make_pair( 0, start ) );
    vector <bool > viz(n  + 1, false);

    while( !heap.empty() ) {
        int nod = heap.top().second;
        heap.pop();

        if( viz[nod] == true ) continue;
        viz[nod] = true;

        for( auto x : graph[nod] ) {
            int cost = x.second;
            int nextNode = x.first;
            if( dis[nextNode] > cost + dis[nod] ) {
                dis[nextNode] = cost + dis[nod];
                heap.push( make_pair(dis[nextNode], nextNode  ) );
            }
        }
    }

}

void read( ) {
    int nrGrafuri;
    f >> nrGrafuri;

    for( int i = 0; i < nrGrafuri; i++ ) {
        int n, m, s;
        f >> n >> m >> s;
        int distante[nmax];
        for( int i = 1; i <= n ;i++ ) f >> distante[i];
        vector< pair <int, int> > graph[nmax];
        for( int j = 0; j < m; j++ ) {
            int a,b ,c;
            f >> a >> b >> c;
            graph[a].push_back( make_pair(b, c) );
            graph[b].push_back( make_pair(a, c) );
        }
        vector <int> distante2(n + 1, INT_MAX );
        distanteDij( graph, distante2, n, s);
        bool ok = true;
        for( int k = 1; k <= n and ok; k++ ) 
            if( distante2[k] != distante[k]  ) {
                g << "NU\n";
                ok = false;
            }
        
        if( ok == true) g << "DA\n";
    }
}

int main() {
    read();
    
    return 0;
}