Cod sursa(job #2586099)

Utilizator Florinos123Gaina Florin Florinos123 Data 19 martie 2020 19:14:20
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

int n, m, start, stop;
int drum[30005];
bool used[30005];
queue < int > coada;
vector < pair < int, int > > muchii[30005];

void Read ()
{
  f >> n >> m >> start >> stop;
  for (int i=1; i<=m; i++)
  {
      int x, y, val;
      f >> x >> y >> val;
      muchii[x].push_back(make_pair(y, val));
      muchii[y].push_back(make_pair(x, val));
  }
}

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

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

int main()
{
  Read();
  Bfs(start);
  g << drum[stop];
    return 0;
}