Cod sursa(job #2888301)

Utilizator ProBatmanBalint Leonard ProBatman Data 10 aprilie 2022 22:18:14
Problema Distante Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.11 kb
#include <bits/stdc++.h>

using namespace std;

string fisier = "distante";

ifstream fin(fisier+".in");
ofstream fout(fisier+".out");

const int CMAX = 5e4+15;
const int oo = INT_MAX;

int n , m , start , x , y , c;
int nr_graf;
int D[CMAX];
bool viz[CMAX];
int distante[CMAX];
vector < pair < int , int > > graf[CMAX];


set < pair < int , int > > S;

void dijkstra(vector < pair < int , int > > graf[], int start)
{
    S.insert({0,start});
    while(!S.empty()){
        int nod_curent = S.begin()->second;
        //cout << "nod curent: " << nod_curent << '\n';
        S.erase(S.begin());
        for(int i=0;i<graf[nod_curent].size();i++)
        {
            int vecin = graf[nod_curent][i].first;
            int cost  = graf[nod_curent][i].second;
            //cout << vecin << "," << cost << " | ";
            if(D[vecin]>D[nod_curent]+cost){
                D[vecin] = D[nod_curent] + cost;
                S.insert({D[vecin],vecin});
            }
        }
        //cout << '\n';
    }
}

bool comparare(int D[], int distante[]){
    for(int i=1;i<=n;i++)
        if(D[i]!=distante[i])
            return false;
    return true;
}

int main()
{
    fin >> nr_graf;
    for(int temp=1;temp<=nr_graf;temp++){
        fin >> n >> m >> start;
        for(int j=1;j<=n;j++){
            fin >> distante[j];
            D[j] = oo;
            viz[j] = 0;
            graf[j].clear();
        }
        for(int j=1;j<=m;j++)
        {
            fin >> x >> y >> c;
            graf[x].push_back({y,c});
            graf[y].push_back({x,c});
        }
        D[start] = 0;
        dijkstra(graf,start);

        /*
        cout << "vectorul de distante dijkstra: " << '\n';
        for(int j=1;j<=n;j++)
            cout << D[j] << " ";
        cout << '\n';
        cout << "vectorul de distante citit" << '\n';
        for(int j=1;j<=n;j++)
            cout << distante[j] << " ";
        cout << '\n';
        */

        if(comparare(D,distante)==false)
            fout << "NU" << '\n';
        else
            fout << "DA" << '\n';

    }
    return 0;
}