Cod sursa(job #2586186)

Utilizator Florinos123Gaina Florin Florinos123 Data 19 martie 2020 22:16:49
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

ifstream f ("distante.in");
ofstream g ("distante.out");

int t;
int n, m, start;
int drum[50005], rez[50005];
vector < pair < int, int > > muchii[50005];
queue < int > coada;

void Clear ()
{
   memset(drum, 0, sizeof(drum));

   for (int i=1; i<=n; i++)
       muchii[i].clear();
}

void Dijktra (int primul)
{
    coada.push(primul);
    while (!coada.empty())
    {
        int nod = coada.front();
        int lg = muchii[nod].size() - 1;
        coada.pop();

        for (int j=0; j<=lg; j++)
        {
            if (drum[muchii[nod][j].first] == 0 && muchii[nod][j].first != primul)
            {
                 drum[muchii[nod][j].first] = drum[nod] + muchii[nod][j].second;
                 coada.push(muchii[nod][j].first);
            }

            if (drum[nod] + muchii[nod][j].second < drum[muchii[nod][j].first])
            {
                 drum[muchii[nod][j].first] = drum[nod] + muchii[nod][j].second;
                 coada.push(muchii[nod][j].first);
            }
        }
    }
}

bool Check ()
{
   for (int i=1; i<=n; i++)
      if (drum[i] != rez[i])
        return false;
   return true;
}

int main()
{
   f >> t;
   for (int i=1; i<=t; i++)
   {
       f >> n >> m >> start;
       for (int i=1; i<=n; i++)
         f >> rez[i];

       for (int j=1; j<=m; j++)
       {
           int x, y, cost;
           f >> x >> y >> cost;
           muchii[x].push_back(make_pair(y, cost));
           muchii[y].push_back(make_pair(x, cost));
       }

       Dijktra(start);

       if (Check())
         g << "DA" << "\n";
       else g << "NU" << "\n";

       Clear();
   }
    return 0;
}