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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

int cost[21][21];
int n, m;
int start, goal, tono, hashira;

int dijkstra_go_village[21];
int dijkstra_go_home[21];

int main() {
  for (int i = 0; i < 21; i++) {
    for (int j = 0; j < 21; j++) {
      cost[i][j] = 999999999;
    }
  }

  while (cin >> n) {
    cin >> m;
    for (int i = 0; i < m; i++) {
      string text;
      cin >> text;

      for (int i = 0; i < text.size(); i++) {
        if (text[i] == ',')
          text[i] = ' ';
      }

      stringstream ss;
      ss << text;

      int a, b, c, d;
      ss >> a >> b >> c >> d;

      cost[a][b] = c;
      cost[b][a] = d;
    }

    string text2;
    cin >> text2;

    for (int i = 0; i < text2.size(); i++) {
      if (text2[i] == ',')
        text2[i] = ' ';
    }

    stringstream ss;
    ss << text2;

    ss >> start >> goal >> tono >> hashira;

    for (int i = 0; i < 21; i++) {
      dijkstra_go_home[i] = 999999;
      dijkstra_go_village[i] = 999999;
    }

    bool flag1[21] = {0};
    dijkstra_go_village[start] = 0;

    int now_place;
    while (1) {
      bool break_flag = true;
      ;
      int min_cost = 9999999;
      for (int i = 1; i <= n; i++) {
        if (flag1[i] == 0 && min_cost > dijkstra_go_village[i]) {
          min_cost = dijkstra_go_village[i];
          now_place = i;
        }
      }

      flag1[now_place] = 1;

      for (int i = 1; i <= n; i++) {
        if (flag1[i] == 0) {
          dijkstra_go_village[i] =
              min(dijkstra_go_village[i],
                  dijkstra_go_village[now_place] + cost[now_place][i]);
          break_flag = false;
        }
      }

      if (break_flag)
        break;
    }

    /////////

    bool flag2[21] = {0};
    dijkstra_go_home[goal] = 0;

    while (1) {
      bool break_flag = true;
      ;
      int min_cost = 9999999;
      for (int i = 1; i <= n; i++) {
        if (flag2[i] == 0 && min_cost > dijkstra_go_home[i]) {
          min_cost = dijkstra_go_home[i];
          now_place = i;
        }
      }

      flag2[now_place] = 1;

      for (int i = 1; i <= n; i++) {
        if (flag2[i] == 0) {
          dijkstra_go_home[i] =
              min(dijkstra_go_home[i],
                  dijkstra_go_home[now_place] + cost[now_place][i]);
          break_flag = false;
        }
      }

      if (break_flag)
        break;
    }

    /*
    for(int i=1; i<=n; i++){
        cout<<dijkstra_go_village[i]<<endl;
    }
    */

    // cout<<dijkstra_go_village[goal]<<endl;
    // cout<<dijkstra_go_home[start]<<endl;
    cout << tono - hashira - dijkstra_go_village[goal] - dijkstra_go_home[start]
         << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

int cost[21][21];
int n, m;

int go_to_home(int start, int goal, int now_place, int now_cost,
               bool village[21]);

int go_to_village(int start, int goal, int now_place, int now_cost,
                  bool village[21]) {
  if (goal == now_place) {

    int min_temp = 9999999 * 20;
    for (int i = 1; i <= n; i++) {
      if (cost[now_place][i] != -1) {
        bool init_village[21] = {0};
        init_village[now_place] = 1;
        init_village[i] = 1;
        min_temp = min(min_temp,
                       go_to_home(goal, start, i, now_cost + cost[now_place][i],
                                  init_village));
      }
    }
    return min_temp;
  }

  int min_temp = 9999999 * 20;
  for (int i = 1; i <= n; i++) {
    if (cost[now_place][i] != -1 && village[i] == 0) {
      bool temp_village[21];
      for (int j = 0; j <= n; j++) {
        temp_village[j] = village[j];
      }
      temp_village[now_place] = 1;
      temp_village[i] = 1;
      min_temp = min(min_temp, go_to_village(start, goal, i,
                                             now_cost + cost[now_place][i],
                                             temp_village));
    }
  }
  return min_temp;
}

int go_to_home(int start, int goal, int now_place, int now_cost,
               bool village[21]) {
  if (goal == now_place) {
    return now_cost;
  }

  int min_temp = 9999999 * 20;
  for (int i = 0; i <= n; i++) {
    if (cost[now_place][i] != -1 && village[i] == 0) {
      bool temp_village[21];
      for (int j = 0; j <= n; j++) {
        temp_village[j] = village[j];
      }
      temp_village[now_place] = 1;
      temp_village[i] = 1;
      min_temp = min(min_temp,
                     go_to_home(start, goal, i, now_cost + cost[now_place][i],
                                temp_village));
    }
  }
  return min_temp;
}

int main() {
  for (int i = 0; i < 21; i++) {
    for (int j = 0; j < 21; j++) {
      cost[i][j] = -1;
    }
  }

  while (cin >> n) {
    cin >> m;
    for (int i = 0; i < m; i++) {
      string text;
      cin >> text;

      for (int i = 0; i < text.size(); i++) {
        if (text[i] == ',')
          text[i] = ' ';
      }

      stringstream ss;
      ss << text;

      int a, b, c, d;
      ss >> a >> b >> c >> d;

      cost[a][b] = c;
      cost[b][a] = d;
    }

    string text2;
    cin >> text2;

    for (int i = 0; i < text2.size(); i++) {
      if (text2[i] == ',')
        text2[i] = ' ';
    }

    stringstream ss;
    ss << text2;

    int x1, x2, y1, y2;
    ss >> x1 >> x2 >> y1 >> y2;

    bool village[21] = {0};
    village[x1] = 1;
    int ans = go_to_village(x1, x2, x1, 0, village);
    cout << y1 - y2 - ans << endl;
  }
}