Mai intai trebuie sa te autentifici.

Cod sursa(job #2604379)

Utilizator CraniXortDumitrescul Eduard CraniXort Data 22 aprilie 2020 15:44:05
Problema Distante Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <bits/stdc++.h>

//std::ifstream fin ("input.txt");
//std::ofstream fout ("output.txt");

std::ifstream fin ("distante.in");
std::ofstream fout ("distante.out");

int *dist_in;
int *dist;
std::vector < std::pair < int, int > > *edge;
std::priority_queue < int, std::vector < std::pair < int, int > >, std::greater < std::pair < int, int > > > pq;

void Dijkstra (){
    while (!pq.empty()){

        int cost = pq.top().first;
        int node = pq.top().second;
        pq.pop();
        //std::cout <<  node << '\n';

        for(int i=0; i<edge[node].size(); i++){
            int next_cost = edge[node][i].second;
            int next_node = edge[node][i].first;
            if (dist[next_node] == 0 or dist[next_node] > cost + next_cost){
                dist[next_node] = cost + next_cost;
                //std::cout << "node: " << next_node << " new value: " << dist[next_node] << '\n';
                pq.push ({dist[next_node], next_node});
            }
        }
    }
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie (NULL);
    std::cout.tie(NULL);


    int Q;
    fin >> Q;
    while (Q--){
        int V, E, src, dest, cost, i;
        fin >> V >> E >> src;
        edge = new std::vector < std::pair < int, int > > [V+5];
        for (i=0; i<=V; i++)
            edge[i].clear();
        dist_in = new int [V+5];
        dist = new int [V+5];
        memset (dist_in, 0, sizeof dist_in);
        memset (dist, 0, sizeof dist);

        pq.push ({0, src});
        dist[src] = -1;

        for (i=1; i<=V; i++)
            fin >> dist_in[i];

        for (i=0; i<E; i++){
            fin >> src >> dest >> cost;
            edge[src].push_back ({dest, cost});
            edge[dest].push_back ({src, cost});
        }

        Dijkstra ();

        bool correct = true;

        for (i=1; i<=V; i++)
            if (dist[i] == -1)
            dist[i] = 0;

        for (i=1; i<=V; i++)
            if (dist[i] != dist_in[i])
                correct = false;

        if (correct == false)
            fout << "NU\n";
        else
            fout << "DA\n";

    }




    return 0;
}