Problem p03602 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
const int N = 1e2 + 5;
const long long INF = 1e18;

int n;
int a[N][N];

priority_queue<pair<int, pair<int, int>>> pq;

struct edge {

  int v;
  int w;

  edge() {}

  edge(int _v, int _w) {
    v = _v;

    w = _w;
  }
};

vector<edge> nb[N];

long long dist[N];

bool vis[N];

void Djikstra(int v) {
  int i;
  int ver, d;

  memset(vis, false, sizeof(vis));

  for (i = 1; i <= n; i++)
    dist[i] = INF;
  dist[v] = 0;

  priority_queue<pair<long long, int>> p;

  p.push(mp(0, v));

  while (!p.empty()) {
    while (vis[p.top().second]) {
      p.pop();

      if (p.empty())
        break;
    }

    if (p.empty())
      break;

    ver = p.top().second;
    d = -p.top().first;

    vis[ver] = true;
    p.pop();

    for (i = 0; i < nb[ver].size(); i++) {
      if (d + nb[ver][i].w < dist[nb[ver][i].v]) {
        p.push(mp(-(d + nb[ver][i].w), nb[ver][i].v));

        dist[nb[ver][i].v] = d + nb[ver][i].w;
      }
    }
  }
}

long long ans;

int main() {
  int i, j;
  int d, u, v;

  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);

      if (i < j)
        pq.push(mp(-a[i][j], mp(i, j)));
    }
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (a[i][j] != a[j][i]) {
        printf("-1\n");

        return 0;
      }
    }
  }

  while (!pq.empty()) {
    d = -pq.top().first;

    u = pq.top().second.first;
    v = pq.top().second.second;

    pq.pop();

    Djikstra(u);

    if (dist[v] == d)
      continue;

    if (dist[v] < d) {
      printf("-1\n");
      return 0;
    }

    if (dist[v] > d) {
      nb[u].pb(edge(v, d));

      nb[v].pb(edge(u, d));

      ans += d;
    }
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
using namespace std;
const int N = 3e2 + 5;
const long long INF = 1e18;

int n;
int a[N][N];

priority_queue<pair<int, pair<int, int>>> pq;

struct edge {

  int v;
  int w;

  edge() {}

  edge(int _v, int _w) {
    v = _v;

    w = _w;
  }
};

vector<edge> nb[N];

long long dist[N];

bool vis[N];

void Djikstra(int v) {
  int i;
  int ver, d;

  memset(vis, false, sizeof(vis));

  for (i = 1; i <= n; i++)
    dist[i] = INF;
  dist[v] = 0;

  priority_queue<pair<long long, int>> p;

  p.push(mp(0, v));

  while (!p.empty()) {
    while (vis[p.top().second]) {
      p.pop();

      if (p.empty())
        break;
    }

    if (p.empty())
      break;

    ver = p.top().second;
    d = -p.top().first;

    vis[ver] = true;
    p.pop();

    for (i = 0; i < nb[ver].size(); i++) {
      if (d + nb[ver][i].w < dist[nb[ver][i].v]) {
        p.push(mp(-(d + nb[ver][i].w), nb[ver][i].v));

        dist[nb[ver][i].v] = d + nb[ver][i].w;
      }
    }
  }
}

long long ans;

int main() {
  int i, j;
  int d, u, v;

  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);

      if (i < j)
        pq.push(mp(-a[i][j], mp(i, j)));
    }
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (a[i][j] != a[j][i]) {
        printf("-1\n");

        return 0;
      }
    }
  }

  while (!pq.empty()) {
    d = -pq.top().first;

    u = pq.top().second.first;
    v = pq.top().second.second;

    pq.pop();

    Djikstra(u);

    if (dist[v] == d)
      continue;

    if (dist[v] < d) {
      printf("-1\n");
      return 0;
    }

    if (dist[v] > d) {
      nb[u].pb(edge(v, d));

      nb[v].pb(edge(u, d));

      ans += d;
    }
  }

  printf("%lld\n", ans);

  return 0;
}