Problem p00485 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int src, dst, cost;
  edge(int src, int dst, int cost) : src(src), dst(dst), cost(cost) {}
};

struct state {
  int cur;
  long long cost;
  state(int cur, long long cost) : cur(cur), cost(cost) {}
  bool operator>(const state &t) const { return cost > t.cost; }
};

typedef vector<edge> edges;
typedef vector<edges> graph;
const int N = 3010;
const long long inf = 1LL << 58;

void dijkstra(const graph &g, int k, int shop[], long long A[]) {
  priority_queue<state, vector<state>, greater<state>> q;
  for (int i = 0; i < k; ++i) {
    state init(shop[i], 0);
    q.push(init);
  }

  while (!q.empty()) {
    state cst = q.top();
    q.pop();

    for (int i = 0; i < (int)g[cst.cur].size(); ++i) {
      // int src = g[cst.cur][i].src;
      int dst = g[cst.cur][i].dst;
      int w = g[cst.cur][i].cost;
      if (A[dst] > cst.cost + w) {
        A[dst] = cst.cost + w;
        q.push(state(dst, cst.cost + w));
      }
    }
  }
}

int main() {
  int n, m, k;
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 & k == 0)
      break;

    long long A[N];
    graph g(n);
    static int adj[N][N];
    for (int i = 0; i < n; ++i) {
      A[i] = inf;
      for (int j = 0; j < n; ++j) {
        adj[i][j] = 0;
      }
    }
    for (int i = 0; i < m; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      adj[a][b] = adj[b][a] = c;
      g[a].push_back(edge(a, b, c));
      g[b].push_back(edge(b, a, c));
    }
    int shop[N];
    for (int i = 0; i < k; ++i) {
      cin >> shop[i];
      --shop[i];
      A[shop[i]] = 0;
    }

    dijkstra(g, k, shop, A);

    double res = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (adj[i][j] > 0) {
          int a = A[i];
          int b = A[j];
          if (b < a)
            swap(a, b);
          // cout << i << ' ' << j << ' ' << a << ' ' << b << ' ' <<
          // ((adj[i][j]+ a -b)/2. + b) << endl;
          res = max(res, (adj[i][j] + a - b) / 2. + b);
        }
      }
    }

    printf("%.0lf\\n", res + 0.499999999);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int src, dst, cost;
  edge(int src, int dst, int cost) : src(src), dst(dst), cost(cost) {}
};

struct state {
  int cur;
  long long cost;
  state(int cur, long long cost) : cur(cur), cost(cost) {}
  bool operator>(const state &t) const { return cost > t.cost; }
};

typedef vector<edge> edges;
typedef vector<edges> graph;
const int N = 3010;
const long long inf = 1LL << 58;

void dijkstra(const graph &g, int k, int shop[], long long A[]) {
  priority_queue<state, vector<state>, greater<state>> q;
  for (int i = 0; i < k; ++i) {
    state init(shop[i], 0);
    q.push(init);
  }

  while (!q.empty()) {
    state cst = q.top();
    q.pop();

    for (int i = 0; i < (int)g[cst.cur].size(); ++i) {
      // int src = g[cst.cur][i].src;
      int dst = g[cst.cur][i].dst;
      int w = g[cst.cur][i].cost;
      if (A[dst] > cst.cost + w) {
        A[dst] = cst.cost + w;
        q.push(state(dst, cst.cost + w));
      }
    }
  }
}

int main() {
  int n, m, k;
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 & k == 0)
      break;

    long long A[n];
    graph g(n);
    int adj[n][n];
    for (int i = 0; i < n; ++i) {
      A[i] = inf;
      for (int j = 0; j < n; ++j) {
        adj[i][j] = 0;
      }
    }
    for (int i = 0; i < m; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      adj[a][b] = adj[b][a] = c;
      g[a].push_back(edge(a, b, c));
      g[b].push_back(edge(b, a, c));
    }
    int shop[N];
    for (int i = 0; i < k; ++i) {
      cin >> shop[i];
      --shop[i];
      A[shop[i]] = 0;
    }

    dijkstra(g, k, shop, A);

    double res = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (adj[i][j] > 0) {
          int a = A[i];
          int b = A[j];
          if (b < a)
            swap(a, b);
          // cout << i << ' ' << j << ' ' << a << ' ' << b << ' ' <<
          // ((adj[i][j]+ a -b)/2. + b) << endl;
          res = max(res, (adj[i][j] + a - b) / 2. + b);
        }
      }
    }

    printf("%.0lf\\n", res + 0.499999999);
  }
}