Cod sursa(job #2830842)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 10 ianuarie 2022 12:13:30
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <fstream>
#include <vector>
#include <climits>
#include <queue>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");

void dijkstra(int start, vector <vector < pair <int, int>>> &adjList, vector <int> &dist, int n){
    //modified version of dijkstra from my class
    vector <bool> viz(n + 1, false);
    dist.clear();
    dist.resize(n + 1, INT_MAX);
    priority_queue <pair <int,int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
    dist[start] = 0;
    q.push(make_pair(0, start));
    while(!q.empty()){
        int current = q.top().second; //nodul minim din queue
        q.pop();
        if(!viz[current]) {
            viz[current] = true;
        }
        else {
            continue;
        }
        unsigned int sz = adjList[current].size();
        for(unsigned int i = 0 ; i < sz; ++i){
            int neighbour = adjList[current][i].first;
            int cost = adjList[current][i].second;
            if(dist[current] + cost < dist[neighbour]){
                dist[neighbour] = dist[current] + cost;
                q.push(make_pair(dist[neighbour], neighbour));
            }
        }

    }

}

void solveProblem(){
    int nodes, edges, startNode;
    f >> nodes >> edges >> startNode;
    vector <int> givenDistances(nodes + 1);
    vector <int> realDistances(nodes + 1);
    vector <vector <pair <int, int>>> adjList(nodes + 1);

    for(int i = 1; i <= nodes; ++i){
        f >> givenDistances[i];
    }

    for(int i = 1; i <= nodes; ++i){
        int a, b, c;
        f >> a >> b >> c;
        adjList[a].push_back(make_pair(b, c));
        adjList[b].push_back(make_pair(a, c));
    }
    dijkstra(startNode, adjList, realDistances, nodes);
    bool givenDistancesAreCorrect = true;
    for(int i = 1; i <= nodes; ++i){
        if(givenDistances[i] != realDistances[i]){
            g << "NU" << "\n";
            givenDistancesAreCorrect = false;
            break;
        }
    }
    if(givenDistancesAreCorrect){
        g << "DA" << "\n";
    }
}

int main() {
    int t;
    f >> t;
    for(int i = 0; i < t; ++i){
        solveProblem();
    }
    return 0;
}