Cod sursa(job #2654417)

Utilizator Snake2003lalallalal Snake2003 Data 30 septembrie 2020 20:41:55
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.68 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>

#define Nmax 50005

using namespace std;

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

vector < int > Numere_Comparatie;
vector < pair < int, int > > Numere_Dijkstra[Nmax];

int Distanta[Nmax];
bool Vizitat[Nmax];

struct comparaElemente
{
    bool operator() (int a, int b)
    {
        return Distanta[a] > Distanta[b];
    }
};

priority_queue < int, vector < int >, comparaElemente > Coada;

int vf;

void Dijkstra(int Nod_Start)
{
    int Nod, Vecin, Cost;

    Coada.push(Nod_Start);
    Distanta[Nod_Start] = 0;
    Vizitat[Nod_Start] = true;

    for(int i = 1; i <= vf; i ++)
        if( i != Nod_Start )
            Distanta[i] = 1e9;

    while( !Coada.empty() )
    {
        Nod = Coada.top();
        Coada.pop();

        Vizitat[Nod] = false;
        for(unsigned int i = 0; i < Numere_Dijkstra[Nod].size(); i ++)
        {
            Vecin = Numere_Dijkstra[Nod][i].first;
            Cost = Numere_Dijkstra[Nod][i].second;
            if( Distanta[Nod] + Cost < Distanta[Vecin] )
            {
                Distanta[Vecin] = Distanta[Nod] + Cost;
                if( !Vizitat[Vecin] )
                {
                    Vizitat[Vecin] = true;
                    Coada.push(Vecin);
                }
            }
        }
    }

}

int main()
{
    int cazuri;
    int ok = 0;
    fin >> cazuri;
    while( cazuri )
    {
        int muchii, nodStart;
        fin >> vf >> muchii >> nodStart;
        for(int i = 1; i <= vf; i ++)
        {
            int x;
            fin >> x;
            Numere_Comparatie.push_back(x);
        }
        for(int i = 1; i <= muchii; i ++)
        {
            int a, b, cost;
            fin >> a >> b >> cost;
            Numere_Dijkstra[a].push_back({b, cost});
            Numere_Dijkstra[b].push_back({a, cost});
        }

        Dijkstra(nodStart);

        for(int i = 1; i <= vf; i ++)
            if( Distanta[i] == Numere_Comparatie[i - 1] )
                ok ++;
            else
            {
                break;
                fout << "NU" << "\n";
            }

        if( ok == Numere_Comparatie.size() )
            fout << "DA" << "\n";
        else
            fout << "NU" << "\n";

        ok = 0;

        memset(Vizitat, false, sizeof(Vizitat));
        memset(Distanta, false, sizeof(Distanta));

        for(int i = 1; i <= vf; i ++)
            Numere_Dijkstra[i].clear();

        Numere_Comparatie.clear();

        while( !Coada.empty() )
            Coada.pop();

        cazuri --;
    }
    return 0;
}