Problem p00212 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A) (A).begin(), (A).end()
#define DUMP(A) cout << #A << "=" << (A) << endl
#define SIZE(A) (int)((A).size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int main() {
  int c, n, m, s, d;
  for (;;) {
    scanf("%d%d%d%d%d", &c, &n, &m, &s, &d);
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0)
      break;
    s--;
    d--;
    int path[100][100];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        path[i][j] = -1;
      }
    }
    int fr, to, co;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &fr, &to, &co);
      fr--;
      to--;
      path[fr][to] = path[to][fr] = co;
    }
    int dist[100][11];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= c; j++) {
        dist[i][j] = -1;
      }
    }
    priority_queue<PP> pque;
    pque.push(PP(0, P(0, s)));
    dist[s][0] = 0;
    while (!pque.empty()) {
      PP now = pque.top();
      pque.pop();
      int ndi = now.first, ncar = now.second.first, nat = now.second.second;
      if (dist[nat][ncar] != -1 && dist[nat][ncar] < ndi)
        continue;
      for (int i = 0; i < n; i++) {
        if (path[nat][i] != -1 &&
            (dist[i][ncar] == -1 || ndi + path[nat][i] < dist[i][ncar])) {
          pque.push(PP(ndi + path[nat][i], P(ncar, i)));
          dist[i][ncar] = ndi + path[nat][i];
        }
        if (path[nat][i] != -1 && ncar + 1 <= c &&
            (dist[i][ncar + 1] == -1 ||
             ndi + path[nat][i] / 2 < dist[i][ncar + 1])) {
          pque.push(PP(ndi + path[nat][i] / 2, P(ncar + 1, i)));
          dist[i][ncar + 1] = ndi + path[nat][i] / 2;
        }
      }
    }
    int mi = INT_MAX;
    for (int i = 0; i <= c; i++) {
      if (dist[d][i] != -1 && dist[d][i] < mi)
        mi = dist[d][i];
    }
    printf("%d\n", mi);
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A) (A).begin(), (A).end()
#define DUMP(A) cout << #A << "=" << (A) << endl
#define SIZE(A) (int)((A).size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int main() {
  int c, n, m, s, d;
  for (;;) {
    scanf("%d%d%d%d%d", &c, &n, &m, &s, &d);
    if (c == 0 && n == 0 && m == 0 && s == 0 && d == 0)
      break;
    s--;
    d--;
    int path[100][100];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        path[i][j] = -1;
      }
    }
    int fr, to, co;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &fr, &to, &co);
      fr--;
      to--;
      path[fr][to] = path[to][fr] = co;
    }
    int dist[100][11];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= c; j++) {
        dist[i][j] = -1;
      }
    }
    priority_queue<PP, vector<PP>, greater<PP>> pque;
    pque.push(PP(0, P(0, s)));
    dist[s][0] = 0;
    while (!pque.empty()) {
      PP now = pque.top();
      pque.pop();
      int ndi = now.first, ncar = now.second.first, nat = now.second.second;
      if (dist[nat][ncar] != -1 && dist[nat][ncar] < ndi)
        continue;
      for (int i = 0; i < n; i++) {
        if (path[nat][i] != -1 &&
            (dist[i][ncar] == -1 || ndi + path[nat][i] < dist[i][ncar])) {
          pque.push(PP(ndi + path[nat][i], P(ncar, i)));
          dist[i][ncar] = ndi + path[nat][i];
        }
        if (path[nat][i] != -1 && ncar + 1 <= c &&
            (dist[i][ncar + 1] == -1 ||
             ndi + path[nat][i] / 2 < dist[i][ncar + 1])) {
          pque.push(PP(ndi + path[nat][i] / 2, P(ncar + 1, i)));
          dist[i][ncar + 1] = ndi + path[nat][i] / 2;
        }
      }
    }
    int mi = INT_MAX;
    for (int i = 0; i <= c; i++) {
      if (dist[d][i] != -1 && dist[d][i] < mi)
        mi = dist[d][i];
    }
    printf("%d\n", mi);
  }
}