Problem p00155 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const double INF_D = 1e18;

const double PI = acos(-1);

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

struct Edge {
  int t;
  double c;
  Edge(){};
  Edge(int t, double c) : t(t), c(c){};
};

int n;
int b[1010], x[1010], y[1010];
vector<Edge> edge[1010];

pdi d[1010];
bool used[1010];
vi ans;

void dijkstra(int s) {
  rep(i, 1010) {
    d[i] = mp(INF_D, -1);
    used[i] = false;
  }
  d[s].fi = 0.0;
  priority_queue<pdi, vector<pdi>, greater<pdi>> q;
  pdi p;
  q.push(mp(0.0, s));
  int from, to;
  double cost;
  while (!q.empty()) {
    p = q.top();
    q.pop();
    from = p.se;
    // cost = p.fi;
    if (d[from].fi < p.fi)
      continue;
    cost = p.fi;
    used[from] = true;
    auto e = edge[from];
    for (int i = 0; i < e.size(); i++) {
      to = e[i].t;
      // if(used[to]) continue;
      if (d[to].fi > cost + e[i].c) {
        // if(to == 11) cout<<"  "<<from<<" "<< cost+e[i].c<<endl;
        d[to].fi = cost + e[i].c;
        d[to].se = from;
        q.push(mp(d[to].fi, to));
      }
    }
  }
}

double getDist2(int x1, int y1, int x2, int y2) {
  int dx = x1 - x2, dy = y1 - y2;
  return sqrt(dx * dx + dy * dy);
}

int main() {
  int m, dx, dy;
  while (cin >> n && n) {
    // cout<<n<<endl;
    rep(i, 1010) {
      b[i] = x[i] = y[i] = -1;
      d[i] = mp(INF_D, -1);
      used[i] = false;
      edge[i].clear();
    }
    rep(i, n) cin >> b[i] >> x[i] >> y[i];
    for (int i = 0; i < 1010; i++) {
      if (b[i] == -1 || x[i] == -1 || y[i] == -1)
        continue;
      for (int j = i + 1; j < 1010; j++) {
        if (b[j] == -1 || x[j] == -1 || y[j] == -1)
          continue;
        dx = x[i] - x[j];
        dy = y[i] - y[j];
        if (dx * dx + dy * dy > 2500)
          continue;
        edge[b[i]].push_back(Edge(b[j], sqrt(dx * dx + dy * dy)));
        edge[b[j]].push_back(Edge(b[i], sqrt(dx * dx + dy * dy)));
      }
    }
    // cout<<edge[1][0].t<<" "<<edge[1][0].c<<endl;
    // cout<<edge[2][0].t<<" "<<edge[2][0].c<<endl;

    cin >> m;
    rep(i, m) {
      int s, g;
      cin >> s >> g;
      // cout<<s<<" "<<g<<endl;
      dijkstra(s);
      if (d[g].fi == INF) {
        cout << "NA" << endl;
        continue;
      }
      ans.clear();
      // bool usedG[1010]={};
      while (1) {
        ans.pb(g);
        if (g == s /*|| g == -1*/)
          break;
        // usedG[g] = true;
        g = d[g].se;
        // if(usedG[g]) break;
        // cout<<"  "<<g<<endl;
      }
      // while(ans.size()> 0 && ans.back()==-1) ans.pop_back();
      reverse(all(ans));
      rep(j, ans.size()) {
        if (j != 0)
          cout << " ";
        cout << ans[j];
      }
      cout << endl;
      // for(int i = 1; i <= 22; i++){
      // 	cout<<" "<<i<<" "<<d[i].fi<<" "<<d[i].se<<endl;
      // }
      // for(int i = 0; i < edge[11].size(); i++){
      // 	if(edge[11][i].t == 6){
      // 		cout<<edge[11][i].c<<endl;
      // 	}
      // }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const double INF_D = 1e18;

const double PI = acos(-1);

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

struct Edge {
  int t;
  double c;
  Edge(){};
  Edge(int t, double c) : t(t), c(c){};
};

int n;
int b[1010], x[1010], y[1010];
vector<Edge> edge[1010];

pdi d[1010];
bool used[1010];
vi ans;

void dijkstra(int s) {
  rep(i, 1010) {
    d[i] = mp(INF_D, -1);
    used[i] = false;
  }
  d[s].fi = 0.0;
  priority_queue<pdi, vector<pdi>, greater<pdi>> q;
  pdi p;
  q.push(mp(0.0, s));
  int from, to;
  double cost;
  while (!q.empty()) {
    p = q.top();
    q.pop();
    from = p.se;
    // cost = p.fi;
    if (d[from].fi < p.fi)
      continue;
    cost = p.fi;
    used[from] = true;
    auto e = edge[from];
    for (int i = 0; i < e.size(); i++) {
      to = e[i].t;
      // if(used[to]) continue;
      if (d[to].fi > cost + e[i].c) {
        // if(to == 11) cout<<"  "<<from<<" "<< cost+e[i].c<<endl;
        d[to].fi = cost + e[i].c;
        d[to].se = from;
        q.push(mp(d[to].fi, to));
      }
    }
  }
}

double getDist2(int x1, int y1, int x2, int y2) {
  int dx = x1 - x2, dy = y1 - y2;
  return sqrt(dx * dx + dy * dy);
}

int main() {
  int m, dx, dy;
  while (cin >> n && n) {
    // cout<<n<<endl;
    rep(i, 1010) {
      b[i] = x[i] = y[i] = -1;
      d[i] = mp(INF_D, -1);
      used[i] = false;
      edge[i].clear();
    }
    rep(i, n) cin >> b[i] >> x[i] >> y[i];
    for (int i = 0; i < 1010; i++) {
      if (b[i] == -1 || x[i] == -1 || y[i] == -1)
        continue;
      for (int j = i + 1; j < 1010; j++) {
        if (b[j] == -1 || x[j] == -1 || y[j] == -1)
          continue;
        dx = x[i] - x[j];
        dy = y[i] - y[j];
        if (dx * dx + dy * dy > 2500)
          continue;
        edge[b[i]].push_back(Edge(b[j], sqrt(dx * dx + dy * dy)));
        edge[b[j]].push_back(Edge(b[i], sqrt(dx * dx + dy * dy)));
      }
    }
    // cout<<edge[1][0].t<<" "<<edge[1][0].c<<endl;
    // cout<<edge[2][0].t<<" "<<edge[2][0].c<<endl;

    cin >> m;
    rep(i, m) {
      int s, g;
      cin >> s >> g;
      // cout<<s<<" "<<g<<endl;
      dijkstra(s);
      if (d[g].fi == INF_D) {
        cout << "NA" << endl;
        continue;
      }
      ans.clear();
      // bool usedG[1010]={};
      while (1) {
        ans.pb(g);
        if (g == s /*|| g == -1*/)
          break;
        // usedG[g] = true;
        g = d[g].se;
        // if(usedG[g]) break;
        // cout<<"  "<<g<<endl;
      }
      // while(ans.size()> 0 && ans.back()==-1) ans.pop_back();
      reverse(all(ans));
      rep(j, ans.size()) {
        if (j != 0)
          cout << " ";
        cout << ans[j];
      }
      cout << endl;
      // for(int i = 1; i <= 22; i++){
      // 	cout<<" "<<i<<" "<<d[i].fi<<" "<<d[i].se<<endl;
      // }
      // for(int i = 0; i < edge[11].size(); i++){
      // 	if(edge[11][i].t == 6){
      // 		cout<<edge[11][i].c<<endl;
      // 	}
      // }
    }
  }
}