Cod sursa(job #3142822)

Utilizator ezluciPirtac Eduard ezluci Data 24 iulie 2023 18:12:47
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
using namespace std;
#ifdef EZ
   #include "./ez/ez.h"
   const string FILE_NAME = "test";
#else
   #include <bits/stdc++.h>
   const string FILE_NAME = "sate";
#endif
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define cin fin
#define cout fout
ifstream fin (FILE_NAME + ".in");
ofstream fout (FILE_NAME + ".out");

const int nMAX = 30e3;

int n, m, x, y;
vector<pair<int, int>> gf[nMAX + 1];
ll d[nMAX + 1];

void shortestPath(int nod)
{
   fill(d + 1, d + n+1, LLONG_MAX>>1);

   queue<pair<int, ll>> q;
   q.push({x, 0});
   d[x] = 0;

   while (!q.empty())
   {
      int nod = q.front().fi;
      ll cost = q.front().se;
      q.pop();

      if (cost != d[nod])
         continue;

      for (auto nex : gf[nod])
         if (d[nod] + nex.se < d[nex.fi])
         {
            d[nex.fi] = d[nod] + nex.se;
            q.push({nex.fi, d[nex.fi]});
         }
   }
}

int main()
{
   cin >> n >> m >> x >> y;
   for (int i = 1; i <= m; ++i)
   {
      int a, b, c;
      cin >> a >> b >> c;
      gf[a].eb(b, c);
      gf[b].eb(a, -c);
   }

   shortestPath(x);

   cout << d[y];
}