Cod sursa(job #2653126)

Utilizator Snake2003lalallalal Snake2003 Data 26 septembrie 2020 22:38:34
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.5 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");

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

vector < pair < int, int > > Numbers[Nmax];
vector < int > Numere;

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

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

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

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

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

        Vizitat[Nod] = false;
        for(unsigned int i = 0; i < Numbers[Nod].size(); i ++)
        {
            Vecin = Numbers[Nod][i].first;
            Cost = Numbers[Nod][i].second;

            if( Distanta[Nod] + Cost < Distanta[Vecin] )
            {
                Distanta[Vecin] = Distanta[Nod] + Cost;
                if( !Vizitat[Vecin] )
                {
                    Coada.push(Vecin);
                    Vizitat[Vecin] = true;
                }
            }
        }
    }

}

int main()
{
    bool ok = true;
    int caz;
    fin >> caz;
    while( caz )
    {
        int vf, muchii, nodStart;
        fin >> vf >> muchii >> nodStart;
        for(int i = 1; i <= vf; i ++)
        {
            int x;
            fin >> x;
            Numere.push_back(x);
        }
        for(int i = 1; i <= muchii; i ++)
        {
            int a, b, cost;
            fin >> a >> b >> cost;
            Numbers[a].push_back({b, cost});
        }
        for(int i = 1; i <= vf; i ++)
            if( i != nodStart )
                Distanta[i] = 1e9;
            else
                Distanta[i] = 0;

        Dijkstra(nodStart);

        for(j = 1; j <= vf; j ++)
        {
           if(Distanta[j] == Numere[j - 1])
            continue;
           else
           {
               ok = false;
               break;
           }
        }
        if( ok )
            fout << "DA" << "\n";
        else
            fout << "NU" << "\n";

        for(int i = 1; i <= vf; i ++)
        Numbers[i].clear();
        Numere.clear();
        memset(Vizitat, 0, sizeof(Vizitat));
        memset(Distanta, 0, sizeof(Distanta));
        while( !Coada.empty() )
            Coada.pop();

        caz --;
    }
    return 0;
}