Problem p00212 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

#define NONE -1

// c: øÌ 1`10
// n: oXªÂÈ®¬Ì 2`100
// m: oXÌHü 1`500
// s: start
// d: goal
int c, n, m, s, d;

// ÅZoHTõ - Ï
vector<vector<int>> edges;

int search() {
  // Å¬l
  vector<vector<int>> D(n, vector<int>(c + 1, NONE));
  // KâÏÝÌm[h
  vector<vector<bool>> V(n, vector<bool>(c + 1, false));

  D[s][c] = 0;

  while (1) {
    int u, w, minv = NONE;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < c + 1; j++) {
        if (V[i][j] == false && D[i][j] != NONE &&
            (minv == NONE || minv > D[i][j])) {
          minv = D[i][j];
          u = i;
          w = j;
        }
      }
    }
    if (minv == NONE)
      break;

    V[u][w] = true;

    for (int v = 0; v < n; v++) {
      if (edges[u][v] == NONE)
        continue;

      if (D[v][w] == NONE)
        D[v][w] = D[u][w] + edges[u][v];
      else
        D[v][w] = min(D[v][w], D[u][w] + edges[u][v]);

      if (w) {
        if (D[v][w - 1] == NONE)
          D[v][w - 1] = D[u][w] + edges[u][v] / 2;
        else
          D[v][w - 1] = min(D[v][w - 1], D[u][w] + edges[u][v] / 2);
      }
    }
  }

  int minv = NONE;
  for (int i = 0; i < c + 1; i++) {
    if (D[d][i] != NONE) {
      if (minv == NONE)
        minv = D[d][i];
      else
        minv = min(minv, D[d][i]);
    }
  }
  return minv;
}

int main(void) {
  while (cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
    edges = vector<vector<int>>(101, vector<int>(101, NONE));
    s--;
    d--;

    // ú»
    for (int i = 0; i < m; i++) {
      // f: a-bÔÌRXg
      int a, b, f;
      cin >> a >> b >> f;

      a--;
      b--;

      edges[a][b] = f;
      edges[b][a] = f;
    }

    // Tõ
    int kotae = search();
    cout << kotae << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define NONE -1

// ÅZoHTõ - Ï
vector<vector<int>> edges;
vector<vector<int>> adj;

vector<int> routes;
vector<int> halfs;
int kotae;
int goal;
int coupon;

// ÅZoHTõ - Ö
void search_routes(int now) {
  if (kotae != NONE) {
    int half = accumulate(halfs.begin(), halfs.end(), 0);
    if (half >= kotae)
      return;
  }

  routes.push_back(now);
  if (now == goal) {
    vector<int> r;
    for (vector<int>::iterator it = routes.begin(); it != routes.end() - 1;
         it++) {
      r.push_back(edges[*(it + 1)][*it]);
    }
    sort(r.rbegin(), r.rend());
    for (int i = 0; i < min(coupon, (int)r.size()); i++) {
      r[i] /= 2;
    }
    int sum = accumulate(r.begin(), r.end(), 0);
    if (kotae == NONE)
      kotae = sum;
    else
      kotae = min(sum, kotae);
  } else {
    for (vector<int>::iterator it = adj[now].begin(); it != adj[now].end();
         it++) {
      if (find(routes.begin(), routes.end(), *it) == routes.end()) {
        halfs.push_back(edges[now][*it] / 2);
        search_routes(*it);
        halfs.pop_back();
      }
    }
  }
  routes.pop_back();
}

int main(void) {
  // c: øÌ 1`10
  // n: oXªÂÈ®¬Ì 2`100
  // m: oXÌHü 1`500
  // s: start
  // d: goal
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && c && n && m && s && d) {
    edges = vector<vector<int>>(501, vector<int>(501, NONE));
    adj = vector<vector<int>>(501, vector<int>());

    s--;
    d--;

    // ú»
    for (int i = 0; i < m; i++) {
      // f: a-bÔÌRXg
      int a, b, f;
      cin >> a >> b >> f;

      a--;
      b--;

      edges[a][b] = f;
      edges[b][a] = f;
      adj[a].push_back(b);
      adj[b].push_back(a);
    }

    routes = vector<int>(0);
    halfs = vector<int>(0);
    kotae = NONE;
    goal = d;
    coupon = c;

    search_routes(s);
    cout << kotae << endl;
  }

  return 0;
}