Problem p03600 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

struct Edge {
  int to, cost;
};
vector<vector<Edge>> es;
typedef pair<long long, int> T;
void dijkstra(int s, int dst, vector<long long> &dist, int v1, int v2) {
  priority_queue<T, vector<T>, greater<T>> pq;
  pq.emplace(0, s);
  dist[s] = 0;
  while (!pq.empty()) {
    T t = pq.top();
    pq.pop();
    long long cost = t.first;
    int v = t.second;
    if (v == dst)
      break;
    for (const auto &e : es[v]) {
      if (v == v1 && e.to == v2 || v == v2 && v1 == e.to)
        continue;
      long long nextcost = cost + e.cost;
      if (dist[e.to] > nextcost) {
        pq.emplace(nextcost, e.to);
        dist[e.to] = nextcost;
      }
    }
  }
}

int main(void) {
  int N;
  cin >> N;
  es.resize(N);
  vector<vector<int>> A(N, vector<int>(N));
  priority_queue<tuple<int, int, int>> pq;

  long long ret = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (i > j) {
        es[i].push_back(Edge{j, A[i][j]});
        es[j].push_back(Edge{i, A[i][j]});
        pq.emplace(A[i][j], i, j);
        ret += A[i][j];
      }
    }
  }
  vector<vector<int>> orgA = A;

  vector<vector<int>> count(N, vector<int>(N));
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
        if (A[i][j] == A[i][k] + A[k][j]) {
          if (i != k && j != k) {
            if (A[i][j] == A[i][k] + A[k][j])
              count[i][j]++;
          }
        }
      }
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i][j] != orgA[i][j])
        ok = false;
    }
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  while (!pq.empty()) {
    vector<long long> dist(N);
    fill(dist.begin(), dist.end(), 1e18);
    tuple<int, int, int> t = pq.top();
    pq.pop();
    int val = get<0>(t);
    int v1 = get<1>(t), v2 = get<2>(t);
    dijkstra(v1, v2, dist, v1, v2);
    if (count[v1][v2]) {
      ret -= val;
    }
  }
  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct Edge {
  int to, cost;
};
vector<vector<Edge>> es;
typedef pair<long long, int> T;
void dijkstra(int s, int dst, vector<long long> &dist, int v1, int v2) {
  priority_queue<T, vector<T>, greater<T>> pq;
  pq.emplace(0, s);
  dist[s] = 0;
  while (!pq.empty()) {
    T t = pq.top();
    pq.pop();
    long long cost = t.first;
    int v = t.second;
    if (v == dst)
      break;
    for (const auto &e : es[v]) {
      if (v == v1 && e.to == v2 || v == v2 && v1 == e.to)
        continue;
      long long nextcost = cost + e.cost;
      if (dist[e.to] > nextcost) {
        pq.emplace(nextcost, e.to);
        dist[e.to] = nextcost;
      }
    }
  }
}

int main(void) {
  int N;
  cin >> N;
  es.resize(N);
  vector<vector<int>> A(N, vector<int>(N));
  priority_queue<tuple<int, int, int>> pq;

  long long ret = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (i > j) {
        es[i].push_back(Edge{j, A[i][j]});
        es[j].push_back(Edge{i, A[i][j]});
        pq.emplace(A[i][j], i, j);
        ret += A[i][j];
      }
    }
  }
  vector<vector<int>> orgA = A;

  vector<vector<int>> count(N, vector<int>(N));
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
        if (A[i][j] == A[i][k] + A[k][j]) {
          if (i != k && j != k) {
            if (A[i][j] == A[i][k] + A[k][j])
              count[i][j]++;
          }
        }
      }
    }
  }
  bool ok = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i][j] != orgA[i][j])
        ok = false;
    }
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  while (!pq.empty()) {
    vector<long long> dist(N);
    fill(dist.begin(), dist.end(), 1e18);
    tuple<int, int, int> t = pq.top();
    pq.pop();
    int val = get<0>(t);
    int v1 = get<1>(t), v2 = get<2>(t);
    // dijkstra(v1, v2, dist, v1, v2);
    if (count[v1][v2]) {
      ret -= val;
    }
  }
  cout << ret << endl;

  return 0;
}