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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;
#define INF 0x3f3f3f3f
int n;

int d[3], d1[3];
char str[3][30] = {"lunch", "dinner", "midnight"};

void solve() {
  int a, b, c, t;
  bool vaild = false;
  memset(d, 0, sizeof(d));
  memset(d1, 0, sizeof(d1));
  for (int i = 0; i < n; i++) {
    scanf("%d:%d %d", &a, &b, &c);
    if (a < 2) {
      a = a + 24;
    }
    //	b += 15;
    vaild = 0;

    if (a >= 11 && a < 15) {
      d1[0]++;
      if (c > b)
        t = c - b;
      else
        t = c + 60 - b;
      if (t <= 8)
        d[0]++;
      // cout<<t<<endl;
    }
    if (a >= 18 && a < 21) {
      d1[1]++;
      if (c > b)
        t = c - b;
      else
        t = c + 60 - b;
      if (t <= 8)
        d[1]++;
    }
    if (a >= 21 && a < 26) {
      d1[2]++;
      if (c > b)
        t = c - b;
      else
        t = c + 60 - b;
      if (t <= 8)
        d[2]++;
    }
  }
  for (int i = 0; i < 3; i++) {
    if (!d1[i])
      printf("%s no guest\\n", str[i]);
    else
      printf("%s %d\\n", str[i], d[i] * 100 / d1[i]);
  }
}

int main() {
  while (scanf("%d", &n), n) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define N 115
#define M 100005
#define ll long long
#define inf 0x7fffffff
#define mod 20100501
#define lson (id << 1)
#define rson (id << 1 | 1)

#define eps 1e-6
#define pii pair<int, int>
#define pdd pair<double, double>
#define It map<unsigned, string>::iterator
#define MP(i, j) make_pair(i, j)

int n, m;
int in[N], out[N];
int pu[N], pv[N];
int pw[N];

int gap[N], dis[N], pre[N], cur[N];
int NE, NV;
int head[N];

struct Node {
  int pos, next;
  ll c;
} E[M];

ll sap(int s, int t) {
  memset(dis, 0, sizeof(dis));
  memset(gap, 0, sizeof(gap));
  for (int i = 0; i < NV; i++)
    cur[i] = head[i];

  int u = pre[s] = s;
  ll aug = -1, maxflow = 0;

  gap[0] = NV;
  while (dis[s] < NV) {
  loop:
    for (int &i = cur[u]; i != -1; i = E[i].next) {
      int v = E[i].pos;
      if (E[i].c > 0 && dis[u] == dis[v] + 1) {
        // checkmin(aug,E[i].c);
        if (aug < 0 || aug > E[i].c)
          aug = E[i].c;
        pre[v] = u;
        u = v;
        if (v == t) {
          maxflow += aug;
          for (u = pre[u]; v != s; v = u, u = pre[u]) {
            E[cur[u]].c -= aug;
            E[cur[u] ^ 1].c += aug;
          }
          aug = -1;
        }
        goto loop;
      }
    }
    int mindis = NV;
    for (int i = head[u]; i != -1; i = E[i].next) {
      int v = E[i].pos;
      if (E[i].c && mindis > dis[v]) {
        cur[u] = i;
        mindis = dis[v];
      }
    }
    if ((--gap[dis[u]]) == 0)
      break;
    gap[dis[u] = mindis + 1]++;
    u = pre[u];
  }
  return maxflow;
}
inline void Insert(int u, int v, ll c) {
  E[NE].c = c;
  E[NE].pos = v;
  E[NE].next = head[u];
  head[u] = NE++;

  E[NE].c = 0;
  E[NE].pos = u;
  E[NE].next = head[v];
  head[v] = NE++;
}

inline void Init() {
  NV = n + 3;
  for (int i = 0; i < NV; i++)
    head[i] = -1;
  NE = 0;
}

int main() {

  int cas, s, t;
  while (scanf("%d%d", &n, &m) != EOF && n + m) {
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));

    ll ans = 0;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &pu[i], &pv[i], &pw[i]);
      if (pw[i] <= 0)
        ans += pw[i];
      else {
        in[pv[i]]++;
        out[pu[i]]++;
      }
    }

    int block = 0;
    for (int i = 0; i < n; i++) {
      if (in[i] == 0 && out[i] == 0)
        block++;
    }

    if (block) {
      printf("%d\\n", ans);
      continue;
    }

    s = n, t = n + 1;
    ll flow = -1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        Init();

        for (int k = 1; k <= m; k++) {
          if (pw[k] > 0) {
            Insert(pu[k], pv[k], pw[k]);
            Insert(pv[k], pu[k], pw[k]);
          }
        }

        Insert(s, i, 9223372036854775807LL);
        Insert(j, t, 9223372036854775807LL);

        ll fuck = sap(s, t);

        // printf("%d->%d:%lld\\n",i,j,fuck);
        if (flow == -1)
          flow = fuck;
        else
          flow = min(flow, fuck);
      }
    }
    printf("%lld\\n", flow + ans);
  }
  return 0;
}