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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

using namespace std;

int minimum, min_past_num, num_ticket, num_town, m, start, goal;

struct Info {
  Info() {
    current_town = 0;
    index = 0;
  }
  bool visited[102];
  int current_town, index, costs[102], visited_town[101];
};

struct Data {
  Data(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  int to, cost;
};

vector<Data> V[101];

void func(Info info) {

  int sum = 0, limit;

  if (info.current_town == goal || minimum != 2000000000) {

    limit = min(num_ticket, info.index);

    for (int i = 0; i < limit; i++) {
      sum += info.costs[i] / 2;
    }

    for (int i = limit; i < info.index; i++) {
      sum += info.costs[i];
    }

    if (info.current_town == goal) {
      minimum = min(minimum, sum);
      return;
    } else {
      if (sum >= minimum) {
        return;
      }
    }
  }

  int calc;
  for (int i = 0; i < V[info.current_town].size(); i++) {
    if (info.visited[V[info.current_town][i].to] == false) {
      Info new_info;
      for (int k = 1; k <= num_town; k++)
        new_info.visited[k] = info.visited[k];
      new_info.visited[V[info.current_town][i].to] = true;
      new_info.current_town = V[info.current_town][i].to;

      if (info.index == 0 || V[info.current_town][i].cost > info.costs[0]) {
        new_info.costs[0] = V[info.current_town][i].cost;
        for (int k = 1; k <= info.index; k++) {
          new_info.costs[k] = info.costs[k - 1];
        }
      } else if (V[info.current_town][i].cost <= info.costs[info.index - 1]) {
        for (int k = 0; k < info.index; k++) {
          new_info.costs[k] = info.costs[k];
        }
        new_info.costs[info.index] = V[info.current_town][i].cost;
      } else {
        for (calc = 0; info.costs[calc] >= V[info.current_town][i].cost; calc++)
          new_info.costs[calc] = info.costs[calc];
        new_info.costs[calc] = V[info.current_town][i].cost;
        calc++;
        for (; calc - 1 < info.index; calc++)
          new_info.costs[calc] = info.costs[calc - 1];
      }
      new_info.index = info.index + 1;
      func(new_info);
    }
  }
}

int main() {

  int from, to, fee;

  while (true) {
    scanf("%d %d %d %d %d", &num_ticket, &num_town, &m, &start, &goal);
    if (num_ticket == 0 && num_town == 0 && m == 0 && start == 0 && goal == 0)
      break;

    minimum = 2000000000;

    for (int i = 1; i <= num_town; i++)
      V[i].clear();

    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &from, &to, &fee);

      V[from].push_back(Data(to, fee));
      V[to].push_back(Data(from, fee));
    }

    Info info;
    for (int i = 1; i <= num_town; i++)
      info.visited[i] = false;

    info.current_town = start;
    info.visited[start] = true;

    func(info);

    printf("%d\\n", minimum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

using namespace std;

int minimum, min_past_num, num_ticket, num_town, m, start, goal,
    costTable[101][101];

struct Info {
  Info() {
    current_town = 0;
    index = 0;
  }
  bool visited[102];
  int current_town, index, costs[102], visited_town[101];
};

void func(Info info) {

  int sum = 0, limit;

  if (info.current_town == goal ||
      (info.index >= min_past_num - 2 && minimum != 2000000000)) {

    limit = min(num_ticket, info.index);

    for (int i = 0; i < limit; i++) {
      sum += info.costs[i] / 2;
    }

    for (int i = limit; i < info.index; i++) {
      sum += info.costs[i];
    }

    if (info.current_town == goal) {
      if (minimum > sum) {
        minimum = sum;
        min_past_num = info.index;
      }
      return;
    } else {
      if (sum >= minimum) {
        return;
      }
    }
  }

  int calc;
  for (int i = 1; i <= num_town; i++) {
    if (costTable[info.current_town][i] != -1 && info.visited[i] == false) {
      Info new_info;
      for (int k = 1; k <= num_town; k++)
        new_info.visited[k] = info.visited[k];
      new_info.visited[i] = true;
      new_info.current_town = i;

      if (costTable[info.current_town][i] > info.costs[0]) {
        new_info.costs[0] = costTable[info.current_town][i];
        for (int k = 1; k <= info.index; k++) {
          new_info.costs[k] = info.costs[k - 1];
        }
      } else if (costTable[info.current_town][i] <=
                 info.costs[info.index - 1]) {
        for (int k = 0; k < info.index; k++) {
          new_info.costs[k] = info.costs[k];
        }
        new_info.costs[info.index] = costTable[info.current_town][i];
      } else {
        for (calc = 0; info.costs[calc] >= costTable[info.current_town][i];
             calc++)
          new_info.costs[calc] = info.costs[calc];
        new_info.costs[calc] = costTable[info.current_town][i];
        calc++;
        for (; calc - 1 < info.index; calc++)
          new_info.costs[calc] = info.costs[calc - 1];
      }
      new_info.index = info.index + 1;
      func(new_info);
    }
  }
}

int main() {

  int from, to, fee;

  while (true) {
    scanf("%d %d %d %d %d", &num_ticket, &num_town, &m, &start, &goal);
    if (num_ticket == 0 && num_town == 0 && m == 0 && start == 0 && goal == 0)
      break;

    minimum = 2000000000;
    min_past_num = 101;

    for (int i = 1; i <= num_town; i++) {
      for (int k = 1; k <= num_town; k++)
        costTable[i][k] = -1;
    }

    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &from, &to, &fee);
      costTable[from][to] = fee;
      costTable[to][from] = fee;
    }

    Info info;
    for (int i = 1; i <= num_town; i++)
      info.visited[i] = false;

    info.current_town = start;
    info.visited[start] = true;

    func(info);

    printf("%d\\n", minimum);
  }

  return 0;
}