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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(it, s)                                                             \\
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define pii pair<int, int>

#define INF 100000000

int dist[310][310];
int dp2[6010];
vector<int> st;
vector<int> e[6010];
vector<int> c[6010];
map<string, int> id;
int n, m, cap;
int to[310];
int from[310];
bool is[6010];
int wh[6010];

void solve(int x) {
  for (int i = 0; i < n; i++)
    dp2[i] = INF;
  for (int i = 0; i < m; i++)
    dist[wh[x]][i] = INF;
  to[wh[x]] = INF;
  from[wh[x]] = INF;
  dp2[x] = 0;
  priority_queue<pair<int, int>> q;
  q.push(mp(0, x));

  while (!q.empty()) {
    int p = q.top().second;
    int cur = -q.top().first;
    q.pop();

    if (is[p] && cur <= cap) {
      dist[wh[x]][wh[p]] = min(cur, dist[wh[x]][wh[p]]);
    }
    if (p == 0)
      from[wh[x]] = min(cur, from[wh[x]]);
    if (p == 1)
      to[wh[x]] = min(cur, to[wh[x]]);
    ;

    for (int i = 0; i < e[p].size(); i++) {
      int u = e[p][i];
      int w = c[p][i];

      if (dp2[u] > w + dp2[p]) {
        dp2[u] = w + dp2[p];
        q.push(make_pair(-dp2[u], u));
      }
    }
  }

  return;
}

int solve2() {
  for (int i = 0; i < n; i++)
    dp2[i] = INF;

  dp2[0] = 0;
  priority_queue<pair<int, int>> q;
  q.push(mp(0, 0));

  while (!q.empty()) {
    int p = q.top().second;
    int cur = -q.top().first;
    q.pop();

    if (p == 1) {
      return cur;
    }

    for (int i = 0; i < e[p].size(); i++) {
      int u = e[p][i];
      int w = c[p][i];

      if (dp2[u] > w + dp2[p]) {
        dp2[u] = w + dp2[p];
        q.push(make_pair(-dp2[u], u));
      }
    }
  }

  return INF;
}

int main() {

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  while (scanf("%d%d%d", &n, &m, &cap) && n != 0) {
    id.clear();
    cap *= 10;
    for (int i = 0; i < 2 * n; i++)
      e[i].clear();
    for (int i = 0; i < 2 * n; i++)
      c[i].clear();
    int cnt = 2;
    st.clear();
    string src, dest;
    cin >> src >> dest;
    id[src] = 0;
    id[dest] = 1;
    for (int i = 0; i < n; i++) {
      string s1, s2;
      cin >> s1 >> s2;
      if (id.find(s1) == id.end())
        id[s1] = cnt++;
      if (id.find(s2) == id.end())
        id[s2] = cnt++;
      int h;
      cin >> h;
      e[id[s1]].push_back(id[s2]);
      e[id[s2]].push_back(id[s1]);
      c[id[s1]].push_back(h);
      c[id[s2]].push_back(h);
    }
    for (int i = 0; i < 2 * n; i++)
      is[i] = 0;
    for (int i = 0; i < 2 * n; i++)
      wh[i] = 0;
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      if (id.find(s) == id.end())
        continue;
      st.push_back(id[s]);
      is[id[s]] = 1;
      wh[id[s]] = i;
    }
    n = cnt;

    for (int i = 0; i < st.size(); i++) {
      solve(st[i]);
    }

    // for (int i=0; i<m; i++) {
    //	for (int j=0; j<m; j++) cout<<dist[i][j]<<" ";
    //	cout<<endl;
    // }

    int ans = solve2();

    if (ans > cap)
      ans = INF;

    for (int k = 0; k < m; k++)
      for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
          dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++) {
        if (from[i] <= cap && to[j] <= cap) {
          ans = min(ans, from[i] + to[j] + dist[i][j]);
        }
      }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(it, s)                                                             \\
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++)
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define pii pair<int, int>

#define INF 1000000000

int d[310];
int dist[310][310];
int dp2[310][6010];
vector<int> st;
vector<int> e[10010];
vector<int> c[10010];
map<string, int> id;
int n, m, cap;
int to[310];
int from[310];
bool is[6010];
int wh[6010];

void solve(int x) {
  for (int i = 0; i < n; i++)
    dp2[x][i] = INF;
  for (int i = 0; i < m; i++)
    dist[wh[x]][i] = INF;
  to[wh[x]] = INF;
  from[wh[x]] = INF;
  dp2[x][x] = 0;
  priority_queue<pair<int, int>> q;
  q.push(mp(0, x));

  while (!q.empty()) {
    int p = q.top().second;
    int cur = -q.top().first;
    q.pop();

    if (is[p] && cur <= cap) {
      dist[wh[x]][wh[p]] = cur;
    }
    if (p == 0)
      from[wh[x]] = cur;
    if (p == 1)
      to[wh[x]] = cur;

    for (int i = 0; i < e[p].size(); i++) {
      int u = e[p][i];
      int w = c[p][i];

      if (dp2[x][u] > w + dp2[x][p]) {
        dp2[x][u] = w + dp2[x][p];
        q.push(make_pair(-dp2[x][u], u));
      }
    }
  }

  return;
}

int solve2() {
  for (int i = 0; i < n; i++)
    dp2[0][i] = INF;

  dp2[0][0] = 0;
  priority_queue<pair<int, int>> q;
  q.push(mp(0, 0));

  while (!q.empty()) {
    int p = q.top().second;
    int cur = -q.top().first;
    q.pop();

    if (p == 1) {
      return cur;
    }

    for (int i = 0; i < e[p].size(); i++) {
      int u = e[p][i];
      int w = c[p][i];

      if (dp2[0][u] > w + dp2[0][p]) {
        dp2[0][u] = w + dp2[0][p];
        q.push(make_pair(-dp2[0][u], u));
      }
    }
  }

  return INF;
}

int main() {

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  while (scanf("%d%d%d", &n, &m, &cap) && n != 0) {
    id.clear();
    cap *= 10;
    for (int i = 0; i < 2 * n; i++)
      e[i].clear();
    int cnt = 2;
    st.clear();
    string src, dest;
    cin >> src >> dest;
    id[src] = 0;
    id[dest] = 1;
    for (int i = 0; i < n; i++) {
      string s1, s2;
      cin >> s1 >> s2;
      if (id.find(s1) == id.end())
        id[s1] = cnt++;
      if (id.find(s2) == id.end())
        id[s2] = cnt++;
      int h;
      cin >> h;
      e[id[s1]].push_back(id[s2]);
      e[id[s2]].push_back(id[s1]);
      c[id[s1]].push_back(h);
      c[id[s2]].push_back(h);
    }
    for (int i = 0; i < 2 * n; i++)
      is[i] = 0;
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      if (id.find(s) == id.end())
        continue;
      st.push_back(id[s]);
      is[id[s]] = 1;
      wh[id[s]] = i;
    }
    n = cnt;

    for (int i = 0; i < st.size(); i++) {
      solve(st[i]);
    }

    // for (int i=0; i<m; i++) {
    //	for (int j=0; j<m; j++) cout<<dist[i][j]<<" ";
    //	cout<<endl;
    // }

    int ans = solve2();

    if (ans > cap)
      ans = INF;

    for (int k = 0; k < m; k++)
      for (int i = 0; i < m; i++)
        for (int j = 0; j < m; j++)
          dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++) {
        if (from[i] <= cap && to[i] >= cap) {
          ans = min(ans, from[i] + to[i] + dist[i][j]);
        }
      }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}