Cod sursa(job #1521158)

Utilizator tc_iuresiures tudor-cristian tc_iures Data 9 noiembrie 2015 22:52:01
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stdlib.h>

using namespace std;

const int Tmax = 15;
const int Nmax = 50005;
const int INF  = 2000000000;

int T, N, M, S;
int D[Nmax], D1[Nmax];
bool ok;
queue<int> Q;
vector<pair<int,int>> G[Nmax];

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

void clearQ()
{
   queue<int> empt;
   swap(empt,Q);
}

void clearG()
{
   for(int i = 1; i <= N; i ++)
   {
      vector<pair<int,int>> empt;
      swap(G[i],empt);
   }
}

int main()
{
    f >> T;
    for(int i = 1; i <= T; i ++)
    {
      clearQ();
      clearG();
      f >> N >> M >> S;
      for(int j = 1; j <= N; j ++)
      {
         f >> D1[j];
         D[j] = INF;
      }
      for(int j = 0; j < M; j ++)
      {
         int x, y, z;
         f >> x >> y >> z;
         G[x].push_back(make_pair(y,z));
         G[y].push_back(make_pair(x,z));
      }
      ok = true;
      D[S] = 0;
      Q.push(S);
      while(!Q.empty())
      {
         int Node = Q.front();
         Q.pop();
         for(int j = 0; j < G[Node].size(); j ++)
         {
            int Ngh = G[Node][j].first;
            int wgt = G[Node][j].second;
            if(D[Ngh] > D[Node] + wgt)
            {
               D[Ngh] = D[Node] + wgt;
               Q.push(Ngh);
            }
         }
      }
      for(int j = 1; j <= N; j ++)
      {
         if(D[j] != D1[j])
         {
            ok = false;
         }
      }
      if(ok)
      {
            g << "Da" << "\n";
      }
      else
      {
         g << "NU" << "\n";
      }

    }
    g.close();
    return 0;
}