Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define INF 9223372036854775807
#define MOD 1000000007
#define pb push_back
#define F first
#define S second
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0;
string s;
bool flag;
#pragma endregion
#define MAX 111111

#define NOT 0
#define VIS 1
#define COMP 2

struct edge {
  ll cost;
  pll node;
  edge(ll c, pll n) : cost(c), node(n) {}
  bool operator>(const edge &e) const { return cost > e.cost; }
};

priority_queue<edge, vector<edge>, greater<edge>> pq1;

ll stt[MAX];
ll mini[MAX];
vector<pll> edge2[MAX];
priority_queue<pll, vector<pll>, greater<pll>> pq2;

void dijkstra(ll start) {
  ll k;
  pll min;
  FOR(i, 0, n) {
    stt[i] = NOT;
    mini[i] = INF;
  }
  mini[start] = 0;
  pq2.push(make_pair(0, start));

  while (!pq2.empty()) {
    min = pq2.top();
    pq2.pop();
    m = min.second;
    stt[m] = COMP;

    if (mini[m] < min.first)
      continue;

    FOR(i, 0, edge2[m].size()) {
      k = edge2[m][i].first;
      if (stt[k] != COMP) {
        if (edge2[m][i].second + mini[m] < mini[k]) {
          mini[k] = edge2[m][i].second + mini[m];
          stt[k] = VIS;
          pq2.push(make_pair(mini[k], k));
        }
      }
    }
  }
}

int main(void) {
  ll x[333][333];
  cin >> n;
  FOR(i, 0, n) {
    FOR(j, 0, n) { cin >> x[i][j]; }
  }
  FOR(i, 0, n) {
    FOR(j, i + 1, n) { pq1.push(edge(x[i][j], pll(i, j))); }
  }

  while (!pq1.empty()) {
    edge e = pq1.top();
    pq1.pop();
    dijkstra(e.node.F);
    // cout << mini[e.node.S] << " " << e.node.first << " " << e.node.second <<
    // endl;
    if (mini[e.node.S] > e.cost) {
      edge2[e.node.F].pb(pll(e.node.S, e.cost));
      edge2[e.node.S].pb(pll(e.node.F, e.cost));
      ans += e.cost;
    } else if (mini[e.node.S] < e.cost) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define INF 9223372036854775807
#define MOD 1000000007
#define pb push_back
#define F first
#define S second
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0;
string s;
bool flag;
#pragma endregion
#define MAX 111111

#define NOT 0
#define VIS 1
#define COMP 2

struct edge {
  ll cost;
  pll node;
  edge(ll c, pll n) : cost(c), node(n) {}
  bool operator>(const edge &e) const { return cost > e.cost; }
};

priority_queue<edge, vector<edge>, greater<edge>> pq1;

ll stt[MAX];
ll mini[MAX];
vector<pll> edge2[MAX];
priority_queue<pll, vector<pll>, greater<pll>> pq2;

void dijkstra(ll start) {
  ll k;
  pll min;
  FOR(i, 0, n) {
    stt[i] = NOT;
    mini[i] = INF;
  }
  mini[start] = 0;
  pq2.push(make_pair(0, start));

  while (!pq2.empty()) {
    min = pq2.top();
    pq2.pop();
    m = min.second;
    stt[m] = COMP;

    if (mini[m] < min.first)
      continue;

    FOR(i, 0, edge2[m].size()) {
      k = edge2[m][i].first;
      if (stt[k] != COMP) {
        if (edge2[m][i].second + mini[m] < mini[k]) {
          mini[k] = edge2[m][i].second + mini[m];
          stt[k] = VIS;
          pq2.push(make_pair(mini[k], k));
        }
      }
    }
  }
}

int main(void) {
  ll x[333][333];
  cin >> n;
  FOR(i, 0, n) {
    FOR(j, 0, n) { scanf("%lld", &x[i][j]); }
  }
  FOR(i, 0, n) {
    FOR(j, i + 1, n) { pq1.push(edge(x[i][j], pll(i, j))); }
  }

  while (!pq1.empty()) {
    edge e = pq1.top();
    pq1.pop();
    dijkstra(e.node.F);
    // cout << mini[e.node.S] << " " << e.node.first << " " << e.node.second <<
    // endl;
    if (mini[e.node.S] > e.cost) {
      edge2[e.node.F].pb(pll(e.node.S, e.cost));
      edge2[e.node.S].pb(pll(e.node.F, e.cost));
      ans += e.cost;
    } else if (mini[e.node.S] < e.cost) {
      cout << "-1" << endl;
      return 0;
    }
  }
  cout << ans << endl;

  return 0;
}